2011-12-14 65 views
-1

我找到了工作,但是最後一個數組,排序C方法輸出這樣的:爲什麼我的輸出重複?

SORTED C METHOD: 179, 181, 238, 190, 105, 144, 26, 63, 90, 14, SORTED C METHOD: 190, 238, 181, 179, 105, 144, 26, 63, 90, 14, SORTED C METHOD: 238, 190, 181, 179, 105, 144, 26, 63, 90, 14, 

爲什麼它如此重複多次,當排序的方法和分類B方法只能輸出一個occurence?
下面是代碼:

主類:

import java.util.ArrayList; 
import java.util.Random; 

public class main { 
    public static void main(String[] args) { 
     ThreeSorts.SortA(randArray(0)); 
     System.out.println("\n"); 
     ThreeSorts.SortB(randArray(0)); 
     System.out.println("\n"); 
     ThreeSorts.SortC(randArray(0)); 
     System.out.println("\n"); 
    } 
    public static ArrayList<Integer> randArray(int n) { 
     ArrayList<Integer> a = new ArrayList<Integer>(n); 
     Random rand = new Random(); 
     rand.setSeed(System.currentTimeMillis()); 

     int[] x = new int[10]; 
     for (int i = 0; i < x.length; i++) { 
      Integer r = Math.abs(rand.nextInt() % 256); 
      a.add(r); 
     } 
     showArray(a); 
     return a; 
    } 

    public static void showArray(ArrayList<Integer> a) { 
     int n = a.size(); 
     System.out.println("RANDOM NUMBERS GENERATED 0 TO 255:"); 
     for (int i = 0; i < n; i++) { 
      int r = a.get(i); 
      System.out.print("|" + r + "| "); 
     } 
     System.out.println("\n"); 
    } 

    public static void showA(ArrayList<Integer> array) { 
     int n = array.size(); 
     System.out.print("SORTED A METHOD: "); 
     for (int i = 0; i < n; i++) { 
      int r = array.get(i); 
      System.out.print(r + ", "); 
     } 
    } 

    public static void showB(ArrayList<Integer> array) { 
     int n = array.size(); 
     System.out.print("SORTED B METHOD: "); 
     for (int i = 0; i < n; i++) { 
      int r = array.get(i); 
      System.out.print(r + ", "); 
     } 
    } 

    public static void showC(ArrayList<Integer> array) { 
     int n = array.size(); 

     System.out.print("SORTED C METHOD: "); 
     for (int i = 0; i < n; i++) { 
      int r = array.get(i); 
      System.out.print(r + ", "); 
     } 
    } 
} 

排序Arrays類:

import java.util.*; 

pubic class ThreeSorts { 
    private static ArrayList<Integer> CopyArray(ArrayList<Integer> a) { 
     ArrayList<Integer> resa = new ArrayList<Integer>(a.size()); 
     for (int i = 0; i < a.size(); ++i) { 
      resa.add(a.get(i)); 
     } 
     return (resa); 
    } 

    public static ArrayList<Integer> SortA(ArrayList<Integer> a) { 
     ArrayList<Integer> array = CopyArray(a); 
     int n = a.size(), i; 
     boolean noswaps = false; 

     while (noswaps == false) { 
      noswaps = true; 
      for (i = 0; i < n - 1; ++i) { 
       if (array.get(i) < array.get(i + 1)) { 
        Integer temp = array.get(i); 
        array.set(i, array.get(i + 1)); 
        array.set(i + 1, temp); 
        noswaps = false; 
       } 
      } 
     } 
     main.showA(array); 
     return (array); 

    } 

    public static ArrayList<Integer> SortB(ArrayList<Integer> a) { 
     ArrayList<Integer> array = CopyArray(a); 
     Integer[] zero = new Integer[a.size()]; 
     Integer[] one = new Integer[a.size()]; 
     int i, b; 
     Integer x, p; 
     // Change from 8 to 32 for whole integers - will run 4 times slower 
     for (b = 0; b < 8; ++b) { 
      int zc = 0; 
      int oc = 0; 
      for (i = 0; i < array.size(); ++i) { 
       x = array.get(i); 
       p = 1 << b; 
       if ((x & p) == 0) { 
        zero[zc++] = array.get(i); 
       } else { 
        one[oc++] = array.get(i); 
       } 
      } 
      for (i = 0; i < oc; ++i) 
       array.set(i, one[i]); 
      for (i = 0; i < zc; ++i) 
       array.set(i + oc, zero[i]); 
     } 
     main.showB(array); 
     return (array); 
    } 

    public static ArrayList<Integer> SortC(ArrayList<Integer> a) { 
     ArrayList<Integer> array = CopyArray(a); 
     SortC(array, 0, array.size() - 1); 
     return (array); 
    } 

    public static void SortC(ArrayList<Integer> array, int first, int last) { 
     if (first < last) { 
      int pivot = PivotList(array, first, last); 
      SortC(array, first, pivot - 1); 
      SortC(array, pivot + 1, last); 
     } 

    } 

    private static void Swap(ArrayList<Integer> array, int a, int b) { 
     Integer temp = array.get(a); 
     array.set(a, array.get(b)); 
     array.set(b, temp); 
    } 

    private static int PivotList(ArrayList<Integer> array, int first, int last) { 
     Integer PivotValue = array.get(first); 
     int PivotPoint = first; 
     for (int index = first + 1; index <= last; ++index) { 
      if (array.get(index) > PivotValue) { 
       PivotPoint = PivotPoint + 1; 
       Swap(array, PivotPoint, index); 
      } 
     } 
     main.showC(array); 
     Swap(array, first, PivotPoint); 
     return (PivotPoint); 
    } 
} 

P.S.我使用Eclipse的Java

+2

感謝您的重新格式化......這讓我的眼睛受傷了! – 2011-12-14 02:51:25

+0

這可能是標記作業嗎? – 2011-12-14 02:53:42

+0

我嘗試了所有的答案,仍然沒有工作:-( – howzat 2011-12-14 03:13:34

回答

0

可能是因爲SortC被稱爲3倍?

public static void SortC(ArrayList<Integer> array,int first,int last){ 
    if (first < last) { 
     int pivot = PivotList(array,first,last); 
     SortC(array,first,pivot-1); 
     SortC(array,pivot+1,last); 
    } 
} 
  • SortC()電話PivotList()
  • PivotList()電話showC()
  • showC()打印到SystemOut

我會更新您的主SortC方法來做,而不是遞歸方法印刷,:

public static ArrayList<Integer> SortC(ArrayList<Integer> a) { 
    ArrayList<Integer> array = CopyArray(a); 
    SortC(array, 0, array.size() - 1); 
    main.showC(array) // print only when finished sorting! 
    return (array); 
} 
1

你的印花十字繡方法調用是issue.Try這樣的:

public static void SortC(ArrayList<Integer> array, int first, int last) { 
    if (first < last) { 
     int pivot = PivotList(array, first, last); 
     SortC(array, first, pivot - 1); 
     SortC(array, pivot + 1, last); 
    } 
    main.showC(array)//this method has to be called only when your array has been sorted 
    } 

的問題與你的代碼,你要打印每次你正在尋找新的支點point.Hope這有助於數組時...快樂編碼

0

正如我所說bunch11給出了正確的答案,這的確是因爲你是你的argorithm,它被稱爲多的時間內用印花十字繡。

但這只是症狀而不是潛在的問題。您的問題在於,您在算法函數(SortedArray類)和實用函數(主類)之間創建了不必要的耦合。

換句話說,你SortedArrays類不應該依賴於主類的東西從而在SortedArrays類沒有應要求在主任何東西。

如果通過調用在主函數顯示功能使這個非常簡單的調整你的代碼,並刪除其中的任何提及在SortedArrays類代碼會顯示每個只有一次。

像這樣:

public static void main(String[] args) { 
     showA(ThreeSorts.SortA(randArray(0))); 
     System.out.println("\n"); 
     showB(ThreeSorts.SortB(randArray(0))); 
     System.out.println("\n"); 
     showC(ThreeSorts.SortC(randArray(0))); 
     System.out.println("\n"); 
    } 

然後將其他類中刪除任何main.showXXX。這假定每個排序函數都返回數組。

這樣,每個類都有一個單一的目的,使一切更容易跟蹤和了解。在相同的功能(或面向對象的同一類)中混合(排序和顯示)通常會導致這種副作用。

最後,我沒有試圖看看算法是否正確,我只關注你的代碼結構。有可能還有其他的錯誤,然後再次,這是一個功課,我不能爲你做所有的工作:-)這說你看起來很好,所以我並不擔心。

1

這一切都與您的public static void SortC(ArrayList<Integer> array,int first,int last) 方法有關。它會被多次呼叫,因爲Recursion

但是,如果您希望以錯誤的方式得到正確的結果,那麼下面的代碼行將幫助您。裏面class ThreeSorts定義這樣一個變量

public static boolean isPrinted; 

和更新您的PivotList()方法,下面給出的或只是複製粘貼任何它將工作。

private static int PivotList(ArrayList<Integer> array, int first, int last) { 
     Integer PivotValue = array.get(first); 
     int PivotPoint = first; 
     for (int index = first + 1; index <= last; ++index) { 
      if (array.get(index) > PivotValue) { 
       PivotPoint = PivotPoint + 1; 
       Swap(array, PivotPoint, index); 
      } 
     } 
     if (!isPrinted) { 
      Main.showC(array); 
      isPrinted = true; 
     } 
     //Main.showC(array); 
     Swap(array, first, PivotPoint); 
     return (PivotPoint); 
    }