2016-11-25 13 views
0

我是相當新到Java。我正在int數組上創建排序算法,以便每個方法都保持它們所做交換和比較次數的計數。我遇到了顯示原始訂單,分揀訂單,交換和比較的麻煩。不知道我需要做些什麼來解決它。任何幫助將不勝感激。Java的泡沫分揀需要數互換和比較

public class IntBubbleSorter { 

    public static void bubbleSort(int[] array) 
    { 
     int lastPos; 
     int index; 
     int temp; 
     int count = 0; 
     int count2 = 0; 

     for (lastPos = array.length - 1; lastPos >= 0; lastPos--) 
      { 
      for (index = 0; index <= lastPos - 1; index++) 
      { 
       count2++; 

       if (array[index] > array[index + 1]) 
       { 
        count++; 
        temp = array[index]; 
        array[index] = array[index + 1]; 
        array[index + 1] = temp; 
       } 
      } 
      } 
     System.out.print("\n Swaps:" + count); 

     System.out.print("\n Comparisons:" + count2); 
    } 
} 


public class SortingTest { 

    public static void main(String[] args) 
    { 
     int[] values = { 1,53,86,21,49,32,90,65,33,11,34,68,54,32,78,80,35,22,96,59,265,44324,123,3123,25435}; 

     System.out.println("Original Order: "); 
     for (int element : values) 
      System.out.print(element + " "); 

     IntBubbleSorter.bubbleSort(values); 

     System.out.println("\nSorted order: "); 
     for (int element : values) 
      System.out.print(element + " "); 

     System.out.println(); 
    } 
} 

//這是它是根據你的意見,你可以讓你的計數變量靜態的,把你輸出的主要方法,像這樣輸出

Original Order: 
1 53 86 21 49 32 90 65 33 11 34 68 54 32 78 80 35 22 96 59 265 44324 123 3123 25435 
Swaps:80 
Comparisons:300 
Sorted order: 
1 11 21 22 32 32 33 34 35 49 53 54 59 65 68 78 80 86 90 96 123 265 3123 25435 44324 
+1

爲什麼你認爲這是不正確的? –

+0

主要方法是讓原來的訂單,但隨後將泡沫分揀和獲得互換和比較,然後回到主方法得到的排序,這不是我想要的,但任何事情我儘量不工作的順序。我將創建更多使用相同主要方法的排序方法,我認爲它會變得非常混亂和混亂。我應該有某種「獲取」方法來獲得交換和比較? – Nosuchluck

+0

這似乎夠合理。您可能會考慮創建某種抽象基類來實現比較和交換功能,然後您的各種排序實現可以繼承通用功能。我相信[塞奇威克(http://www.informit.com/store/algorithms-in-java-parts-1-4-9780201361209)採取了這種做法。 –

回答

0

public class IntBubbleSorter { 

    public static int count = 0; 

    public static int count2 = 0; 

    public static void bubbleSort(int[] array) 
    { 
     int lastPos; 
     int index; 
     int temp; 
     count = 0; 
     count2 = 0; 

     for (lastPos = array.length - 1; lastPos >= 0; lastPos--) 
      { 
      for (index = 0; index <= lastPos - 1; index++) 
      { 
       count2++; 

       if (array[index] > array[index + 1]) 
       { 
        count++; 
        temp = array[index]; 
        array[index] = array[index + 1]; 
        array[index + 1] = temp; 
       } 
      } 
      } 
    } 
} 


public class SortingTest { 

    public static void main(String[] args) 
    { 
     int[] values = { 1,53,86,21,49,32,90,65,33,11,34,68,54,32,78,80,35,22,96,59,265,44324,123,3123,25435}; 

     System.out.println("Original Order: "); 
     for (int element : values) 
      System.out.print(element + " "); 

     IntBubbleSorter.bubbleSort(values); 

     System.out.println("\nSorted order: "); 
     for (int element : values) 
      System.out.print(element + " "); 

     System.out.println(); 

     System.out.print("\n Swaps:" + IntBubbleSorter.count); 

     System.out.print("\n Comparisons:" + IntBubbleSorter.count2); 
    } 
} 

但我應該提及static應小心使用。

+0

我已經嘗試過這一點,但詮釋主要你有System.out.print(「\ n掉期:」+計數);和System.out.print(「\ n比較:」+ count2);我收到「count」和「count2」的錯誤。它希望我爲它們創建一個局部變量或字段。 – Nosuchluck

+0

@ ducatigirl74我的壞,我很樂意回答,我錯過了你的代碼示例包含了兩個不同的班級。現在應該工作。 – Ayutac

+0

看起來像它會工作,但我仍然收到錯誤。 「計數的變化可見性(或計數2)@ ducatigirl74這是奇怪的包裝或爲他們創造getter和setter。 – Nosuchluck