[英]Why is my bubble sort giving an incorrect output when sorting data in ascending order?
我已經在Java中創建了冒泡排序算法的實現。 該代碼運行良好並提供了有效的輸出,但是由於某種原因,當我按升序對數據進行排序時,它可以正常工作,但是在嘗試打印該語句時出現了問題。 下面是我的代碼,以及對該問題的稍微更好的描述!
import java.util.Arrays;
import java.util.Scanner;
//import java.util.regex.Pattern;
//import java.util.stream.Stream;
public class BubbleSortNumeric {
public static void main (String [] args) {
Integer [] unsortedData = getDataInput();
Integer [] sortedDataAscending;
Integer [] sortedDataDescending;
long start = System.nanoTime();
sortedDataAscending = bubbleSortAscending(unsortedData);
sortedDataDescending = bubbleSortDescending(unsortedData);
long stop = System.nanoTime();
System.out.println("Ascending: " + Arrays.toString(sortedDataAscending));
System.out.println("Descening: " + Arrays.toString(sortedDataDescending));
System.out.println("Execution time: " + ((stop - start) / 1e+6) + "ms.");
}
private static Integer [] getDataInput() {
System.out.println("Enter a set of integers seperated by a space.");
Integer [] userInput = {};
String strInput;
try(Scanner sc = new Scanner(System.in)) {
strInput = sc.nextLine();
}
String [] inputData = strInput.split("\\s+");
try {
userInput = Arrays.asList(inputData).stream().map(Integer::valueOf).toArray(Integer[]::new);
}catch(NumberFormatException e) {
System.out.println("ERROR. Invalid input.\n" + e.getMessage());
}
return userInput;
}
private static Integer [] bubbleSortAscending(Integer[] ascendingUnsorted) {
int n = ascendingUnsorted.length;
System.out.println(n);
if(n == 1) {
return ascendingUnsorted;
}
boolean swapped;
int temp;
do {
swapped = false;
for(int i = 1; i < n; i++) {
if(ascendingUnsorted[i - 1] > ascendingUnsorted[i]) {
temp = ascendingUnsorted[i - 1];
ascendingUnsorted[i - 1] = ascendingUnsorted[i];
ascendingUnsorted[i] = temp;
swapped = true;
}
}
n--;
}while(swapped == true);
return ascendingUnsorted;
}
private static Integer [] bubbleSortDescending(Integer [] descendingUnsorted) {
int n = descendingUnsorted.length;
if(n == 1) {
return descendingUnsorted;
}
boolean swapped;
int temp;
do {
swapped = false;
for(int i = 1; i < n; i++) {
if(descendingUnsorted[i - 1] < descendingUnsorted[i]) {
temp = descendingUnsorted[i];
descendingUnsorted[i] = descendingUnsorted[i - 1];
descendingUnsorted[i - 1] = temp;
swapped = true;
}
}
n--;
}while(swapped == true);
return descendingUnsorted;
}
}
當我調用bubbleSortAscending
它可以正常工作,並按升序對數據進行排序。 因為我正在計時程序的執行時間,所以我不想在按降序對數據進行排序之前先打印出結果。
我的問題是,盡管兩種方法都可以正常工作,但在打印結果時還是有問題。 下面是發生的情況的一個示例:
輸入項
1 3 9 2 40 193
輸出:
升序:[193、40、9、3、2、1]
降序:[193、40、9、3、2、1]
執行時間:0.527142ms。
如果我要將print語句移到sortedDataAscending = bubbleSortAscending(unsortedData);
那么它將給出正確的輸出,但是,正如我已經說過的那樣,我不想要那樣。
所以我的問題是,即使我將結果分配給兩個不同的變量,為什么當我打印兩個變量的答案時,輸出是否相同?
您必須制作輸入數組的副本,因為在Java中,您將值作為引用而不是副本傳遞:
public class BubbleSortNumeric {
public static void main (String [] args) {
Integer [] unsortedData1 = getDataInput();
Integer [] unsortedData2 = new Integer[unsortedData1.length];
System.arraycopy(unsortedData1, 0, unsortedData2, 0, unsortedData1.length);
Integer [] sortedDataAscending;
Integer [] sortedDataDescending;
long start = System.nanoTime();
sortedDataAscending = bubbleSortAscending(unsortedData1);
sortedDataDescending = bubbleSortDescending(unsortedData2);
// ...
經過測試,您的算法很好,只有數組錯誤:
$ javac BubbleSortNumeric.java && java BubbleSortNumeric
Enter a set of integers seperated by a space.
1 3 9 2 40 193
6
Ascending: [1, 2, 3, 9, 40, 193]
Descening: [193, 40, 9, 3, 2, 1]
Execution time: 0.068779ms.
我會給您一個提示:交換這兩行,執行,您將看到魔術。 由此
sortedDataAscending = bubbleSortAscending(unsortedData);
sortedDataDescending = bubbleSortDescending(unsortedData);
對此
sortedDataDescending = bubbleSortDescending(unsortedData);
sortedDataAscending = bubbleSortAscending(unsortedData);
這對您有什么建議?
。
。
。
。
您要修改同一數組兩次! 您所有的參考
unsortedData
sortedDataAscending
sortedDataDescending
指向同一對象:一個數組,該數組首先未排序,然后按升序排序,最后按降序排序。
此時,您決定將其打印出來。
兩次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.