2015-05-22 43 views
0

。基本上下面的代碼所做的(假設)是,創建一組非重複的隨機數,將它們填充到一個數組中,該數組被轉換爲一個列表並將其排序。問題是嵌套for循環,我管理一個工作,但不知道它是如何工作的。經常,我似乎無法正確排序,重複出現的情況以及不時出現的界限錯誤。Java排序循環不起作用

如何代碼工作:

  1. 生成不重複的隨機數
  2. 裝滿他們的陣列
  3. 使用嵌套的循環來找到最小值
  4. 請插入到一個新的數組
  5. 將其從第一個陣列中刪除
  6. 重複最後2個步驟,直到第一個數組爲空,第二個陣列 OS填充在排序順序

    import org.apache.commons.lang.ArrayUtils; 
    import java.util.ArrayList; 
    import java.util.Arrays; 
    import java.util.*; 
    import java.lang.*; 
    import java.io.*; 
    
    public class Sorter { 
    
    public static void main(String[] args) { 
    int[] process = fillArray(20,1,25); 
    sorter(process,20); 
    
    } 
    
    public static int[] sorter(int array[],int size) { 
    
    int[] useArray = array; 
    
    Integer[] newArray = ArrayUtils.toObject(useArray); 
    List<Integer> arrayList = new ArrayList(Arrays.asList(newArray)); 
    
    
    //System.out.println((arrayList)); 
    
    int counter = 1; 
    int minval = 0; 
    int diffsize = size - 1; 
    int actualVal = 0; 
    int storeArray[] = new int[size]; 
    int removeIndex =0; 
    
    Integer[] newStore = ArrayUtils.toObject(storeArray); 
    List<Integer> storeList = new ArrayList(Arrays.asList(newStore)); 
    
    System.out.println((arrayList)); 
    
    // Both loops messed up 
    for (int i = 0; i < size+diffsize; i++) { 
    
        for (int n = 0; n < size-1; n++) { 
    
         if (arrayList.get(minval) < arrayList.get(counter)) { 
          actualVal = arrayList.get(minval); 
          System.out.println((arrayList.get(minval)) + " Less than " + arrayList.get(counter)); 
          counter = counter + 1; 
          removeIndex = minval; 
         } else { 
          actualVal = arrayList.get(counter); 
          System.out.println((arrayList.get(counter)) + " Less than " + arrayList.get(minval)); 
          minval = counter; 
          counter = counter + 1; 
          removeIndex = counter; 
         } 
        } 
    
        // System.out.println(actualVal); 
        storeList.add(actualVal); 
        arrayList.remove(actualVal); // need to remove the smallest value to repeat the sorting and get the next smallest value, but this is not removing it 
        size = size - 1; 
        counter = 1; 
        minval = 0; 
        // if (i + size == i) { 
        //  storeList.set(i, arrayList.get(0)); 
        // } 
        // System.out.println(removeIndex); 
    
        // System.out.println(arrayList); 
    } 
    
    
        // System.out.println(storeList); 
    
         int[] ints = new int[storeList.size()]; 
         int d = 0; 
         for (Integer u : storeList) { 
         ints[d++] = u; 
         } 
    
    
        return ints; 
    } 
    
    
        public static int randomNum(int lower,int upper){ 
        Random rand = new Random(); 
        int randomNum = lower + rand.nextInt((upper- lower) + 1); 
        return randomNum; 
    } 
    
    
    
    
    public static int[] fillArray(int size,int lowerBound,int upperBound){ 
        int holdArray[] = new int[size]; 
    
        int rand = 0; 
    
        for (int count =0;count < holdArray.length;count++){ 
         holdArray[count] = 0; 
        } 
    
        for (int count =0;count < holdArray.length;count++){ 
    
         rand = randomNum(lowerBound,upperBound); 
         if (ArrayUtils.contains(holdArray, rand)) { 
          while (ArrayUtils.contains(holdArray, rand)) { 
          rand = randomNum(0, 20); 
         } 
        } 
        holdArray[count] = rand; 
    } 
        // System.out.println(Arrays.toString(holdArray)); 
    
    //return holdArray; 
    
    return holdArray; 
    
    } 
    
    
    
    } 
    
+0

而(ArrayUtils.contains(holdArray,RAND)){ 蘭特= randomNum(0,20); }你沒有使用你的界限。 –

+1

分而治之。先寫一個簡單的排序函數並測試一下。你不想使用內置的排序方法,所以你可以學習對嗎?閱讀泡沫排序或類似的東西,並首先實現。然後其餘的會更容易。 –

+0

我不明白。你有四個不同的'Integer []'數組,三個'int []'數組*和*兩個'ArrayList's。其中一些是原始數組的副本,其中一些填充了'null'或零(它們都不爲空)。目前還不清楚在排序過程中您實際正在閱讀的內容,但很明顯,附加到非空列表無法產生正確的結果。 – Holger

回答

1

你可以給出一個令人信服的理由來證明從數組轉換爲列表是合理的嗎?爲什麼不能只使用列表或只使用數組?我在下面的答案中使用ArrayList;

首先是fillArray類。你不需要填滿所有的零。爲什麼還要費心去填充一個你會替換的值?

public static ArrayList<Integer> fillArray(int size,int lowerBound,int upperBound){ 
    ArrayList<Integer> a = new ArrayList<Integer>(size); 
    for (int count =0;count < size;count++){ 
     Integer rand = new Integer(randomNum(lowerBound,upperBound)); 
     a.add(rand); 
    } 
    return a; 
} 

二,排序類。你說的方法,尋找最低的價值,然後做魔術的東西,什麼不是。

public static ArrayList<Integer> sorter(ArrayList<Integer> unsorted) { 
ArrayList<Integer> sortedArray = new ArrayList<Integer>(unsorted.size()); 
while(!unsorted.isEmpty()) { //repeats until the unsorted list is empty 
    int minval = unsorted.get(0); 
    int removeIndex = 0; 
    for(int i=1;i<unsorted.size();i++) 
     if (unsorted.get(i)<minval) { 
      minval = unsorted.get(i); 
      removeIndex = i; 
     } 
    sortedArray.add(minval); 
    unsorted.remove(removeIndex); 
    } 
    return sortedArray; 
} 

主方法,以測試它

public static void main(String[] args) { 
    ArrayList<Integer> a = fillArray(20,1,25); 
    System.out.println("unsorted array"); 
    for (Integer c : a) 
     System.out.print(c + ";"); 
    ArrayList<Integer> b = sorter(a); 
    System.out.println("\nnew unsorted array"); 
    for (Integer c : a) 
     System.out.print(c + ";"); 
    System.out.println("\nsorted array"); 
    for (Integer c : b) 
     System.out.print(c + ";"); 
} 

此輸出

unsorted array 
22;2;23;22;13;12;4;1;7;14;25;18;9;12;3;8;20;3;1;20; 
new unsorted array 

sorted array 
1;1;2;3;3;4;7;8;9;12;12;13;14;18;20;20;22;22;23;25; 
+0

謝謝,這清除了很多東西。我之所以選擇列表的原因是,數組大小是固定的,無法擴展,在哪裏我可以更輕鬆地列出manupilate。 – user3227275

+0

好吧,但ArrayList在這裏幾乎不需要。由於我們在調用fillArray時已經在開始時定義了數組的大小,因此ArrayList和Array的實現將與您的情況相同。 – Isaac

+0

因此,您認爲哪個性能更好,更簡單,功能更強? – user3227275

1

分離出「尋找最小」,並從數組「插入/缺失」爲兩種方法,然後使用它們。

這樣的代碼將更易於管理。以下是find_min方法的示例。

int find_min(int[] array, int start) { 
    int min = Integer.MAX_VALUE; 
    for(int i = start; i < array.length; ++i) 
     if(array[i] < min) 
      min = array[i] ; 
    return min; 
} 

現在在排序例程,使用find_min找到最小元素並將其插入到新陣列,然後從原始數組中刪除的最小元素。但是,此方法不會返回最小元素的索引。所以,我建議你修改它以返回索引和元素作爲一對int值。

你整理程序將是這個樣子:

new_array := [] 
while(length(original_array) > 0) 
    min, min_index := find_min(original_array) 
    new_array.append(min) 
    original_array.delete(min_index) 

您可以使用像這樣的東西返回int對:

class IntPair { 
    int min; 
    int index; 

    public IntPair(int x, int y) { this.min=x; this.index=y; } 

    public int get_min() { return min; } 
    public int get_min_index() { return index; } 
} 

而且,因爲你會去這樣做的插入和刪除,改爲使用ArrayList。它具有刪除特定索引處的元素並向其添加元素的方法。

注:列出你的做法是從陣列右側接近Selection Sort算法中,我們將陣列分成兩個部分(分類左側部分和未分類的右邊部分),反覆挑最小的元素,並用其交換左邊的最右邊的元素。在這種情況下,你不需要兩個數組,你不需要刪除或插入元素到數組中。