我用 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
输出:
Ascending: [193, 40, 9, 3, 2, 1]
Descening: [193, 40, 9, 3, 2, 1]
Execution time: 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.
关于java - 按升序对数据进行排序时,为什么冒泡排序会给出不正确的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32540078/