バブルソートアルゴリズムをJavaで作成しようとしていますが、何も返さずにソートしなければならないときは、プログラムが実行されると、それはその何も起こらないしかし後に限り、ソートする前に、アレイを印刷するとして取得しますが、プログラムは、それがJavaの私のバブルソートアルゴリズムはソートを停止せず、何も返さない
package src;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
public class bubbleSort {
public static void main(String[] args) {
int length = getLength();
List<Integer> randomList = createList(length);
System.out.println("The list before sorting:\n" + randomList);
List<Integer> newList = sortList(randomList, length);
System.out.println("The list after sorting:\n" + newList);
}
public static int getLength() {
System.out.println("Please enter how long you want the array to be");
Scanner reader = new Scanner(System.in);
int length = Integer.parseInt(reader.nextLine());
return length;
}
public static List<Integer> createList(int length) {
Random rand = new Random();
List<Integer> randomList = new ArrayList<Integer>();
for(int x = 0 ; x < length ; x++){
int randomnumber = rand.nextInt((100 - 1) + 1) + 1;
randomList.add(randomnumber);
}
return randomList;
}
public static List<Integer> sortList(List<Integer> randomList, int length){
boolean sorted = false;
while(sorted == false){
sorted = true;
for(int x = 0 ; x < (length - 1) ; x++) {
if(randomList.get(x) > randomList.get(x + 1)) {
sorted = false;
int temp = randomList.get(x + 1);
randomList.set((x + 1), (x));
randomList.set((x + 1), temp);
}
}
}
return randomList;
}
}
'randomList.set((X + 1)、(x)); 'それはむしろ' randomList.set((x + 1)、randomList.get(x)); ' –
これは次のようなものです:while(sorted == false){ sorted = true; ? – Stultuske
スワップコードが間違っていないか確認してください。 int temp = randomList。get(x + 1); randomList.set((x + 1)、(x)); randomList.set((x + 1)、temp); –