方法bubblesort(家庭作业)

时间:2016-05-30 01:18:48

标签: java arraylist

好吧,我已经尝试了一段时间了,我无法弄明白。基本上我的问题是当代码运行并到达第一个if (Bubblesort.get(j))时 自动跳转到另一个if,不更改值并重复直到排序。有什么想法吗?

package APS;
import java.util.ArrayList;
import APS.RandomArray;
public class BubbleSort {

    BubbleSort(){
        RandomArray Ordenar = new RandomArray();
        ArrayList<Integer> Bubblesort = Ordenar.vetores();
        boolean controle;

        for (int i = 0; i < Bubblesort.size()-1; i++){
            controle = true;
            for (int j =0; j < (Bubblesort.size()-i); j++){
                if (Bubblesort.get(j).compareTo(Bubblesort.get(j+1))>0){
                    Integer a = Bubblesort.get(j);
                    Bubblesort.set(i, Bubblesort.get(j+1));
                    Bubblesort.set(j+1, a);
                    controle = false;
                }
                if (controle){
                    break;
                }

            }

        }
        for(int i = 0; i < Bubblesort.size();i++){
            System.out.println(Bubblesort.get(i));
        }

    }
}

2 个答案:

答案 0 :(得分:0)

您的代码,进行了以下更改,前三条如前面的评论中所述,另一条用于制作代码MCVE(Minimal, Complete, and Verifiable):

  • if (controle)移到内部循环外。
  • Bubblesort.set(i,更改为Bubblesort.set(j,
  • j < (Bubblesort.size()-i)更改为j < (Bubblesort.size()-i-1)
  • MCVE:将ArrayList<Integer> Bubblesort更改为参数。
class BubbleSort {
    BubbleSort(ArrayList<Integer> Bubblesort){
        boolean controle;

        for (int i = 0; i < Bubblesort.size()-1; i++){
            controle = true;
            for (int j =0; j < (Bubblesort.size()-i-1); j++){
                if (Bubblesort.get(j).compareTo(Bubblesort.get(j+1))>0){
                    Integer a = Bubblesort.get(j);
                    Bubblesort.set(j, Bubblesort.get(j+1));
                    Bubblesort.set(j+1, a);
                    controle = false;
                }
            }
            if (controle){
                break;
            }
        }
        for(int i = 0; i < Bubblesort.size();i++){
            System.out.println(Bubblesort.get(i));
        }
    }
}

<强> TEST

new BubbleSort(new ArrayList<>(Arrays.asList(1,5,2,4,3)));

<强>输出

1
2
3
4
5

对我来说这看起来很不错。

答案 1 :(得分:-1)

你忘记改变控制状态。如果在break;

之前,请在第二句中添加语句class BubbleSort { BubbleSort(ArrayList<Integer>
    for (int i = 0; i < Bubblesort.size()-1; i++){
        controle = true;
        for (int j =0; j < (Bubblesort.size()-i-1); j++){
            if (Bubblesort.get(j).compareTo(Bubblesort.get(j+1))>0){
                Integer a = Bubblesort.get(j);
                Bubblesort.set(j, Bubblesort.get(j+1));
                Bubblesort.set(j+1, a);
                controle = false;
            }
        }
        if (controle){
            controle = true;
            break;
        }
    }
    for(int i = 0; i < Bubblesort.size();i++){
        System.out.println(Bubblesort.get(i));
    }
}

冒泡){         布尔控制;

counter = 0

while counter == 0:

    print "You enter a dark room with two doors.  Do you go through door #1 or door #2?"

    door = raw_input("> ")

    if door == "1":
        print "There's a giant bear here eating a cheese cake.  What do you do?"
        print "1. Take the cake."
        print "2. Scream at the bear."

        bear = raw_input("> ")

        if bear == "1":
            print "The bear eats your face off.  Good job!"
        elif bear == "2":
            print "The bear eats your legs off.  Good job!"
        else:
            print "Well, doing %s is probably better.  Bear runs away." % bear
            break

    else:
        print "You stumble around and fall on a knife and die.  Good job!"

}