2015-09-12 26 views
0

我已經在Java中創建了Bubble排序算法的實現。代碼運行良好,並提供有效的輸出,但是,由於某種原因,當我按升序對數據進行排序時,它正在正確地執行此操作,但當我嘗試打印語句時遇到問題。下面是我的代碼,以及對問題的更好描述!爲什麼當按升序排序數據時,我的氣泡排序會給出錯誤的輸出?

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它工作正常,並以升序排序數據。因爲我正在計劃程序的執行時間,所以在按降序對數據進行排序之前,我不想打印出結果。

我的問題是,雖然這兩種方法都能正常工作,但在打印結果時出現問題。會發生什麼的一個例子是下面:

輸入

輸出:

升序:[193,40,9,3, 2,1]

下降:[193,40,9,3,2,1]

執行時間:0.527142ms。

如果我然而,移動打印語句只是行sortedDataAscending = bubbleSortAscending(unsortedData);後,那麼它會給出正確的輸出,正如我已經說過,我不希望出現這種情況。

所以我的問題,即使我將結果分配給兩個不同的變量,爲什麼當我打印這兩個變量的答案時,輸出是相同的呢?

+0

在你排序方法,您實際上對參數數組進行排序,因此您可以更改作爲參數提供的實際對象。 – AndrewMcCoist

回答

3

因爲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); 
     // ... 

測試,你algorythm工作得很好,只有陣列錯誤:

$ 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. 
0

我給你一個提示:交換這兩行,執行,你會看到魔法。 從這個

sortedDataAscending = bubbleSortAscending(unsortedData); 
    sortedDataDescending = bubbleSortDescending(unsortedData); 

這個

sortedDataDescending = bubbleSortDescending(unsortedData); 
    sortedDataAscending = bubbleSortAscending(unsortedData); 

這是什麼建議你?

你正在修改兩次相同的數組!所有的證明

unsortedData 
sortedDataAscending 
sortedDataDescending 

都指向同一個對象:一個數組,即先不排序,然後按升序排序,最後按降序排列。

此時您決定打印出來。

兩次。