2017-02-16 148 views
-4

我正在編寫一個類的程序來顯示編碼冒泡排序的能力。我一直在努力工作好幾天,似乎無法得到它。至少現在它編譯,但拋出一個異常。
我評論了我遇到問題的部分,實際交換數組中的元素。冒泡排序拋出異常

該程序應該生成20個隨機整數的數組,然後使用冒泡排序對它們進行排序,打印每一遍,直到它完成。

import java.util.*; 


public class BubbleSorting { 


public static void bubbleSort(ArrayList<Integer> arr) { 
    int n = arr.size(); 
    int temp = 0; 

    for (int i = 0; i < n; i++) { 

    //this is the chunk of code that I am having problems with 
    for (int j = i; j < (n-1); j++) { 
     if (arr.get(n-1) < arr.get(j)) 
      temp = arr.get(j-1); 
      arr.set(j-1, arr.get(j)); 
      arr.set(j, temp); 
    } 

    } 
} 

private static void printOut(int pass, ArrayList<Integer> array) { 
    System.out.print("Pass " + pass + ": "); 
    for (int i = 0; i < array.size() - 1; i++) { 
    System.out.print(array.get(i) + ", "); 
    } 

    System.out.print(array.get(array.size() - 1) + "."); 
    System.out.println(); 


} 


public static void main(String[] args) { 
    ArrayList<Integer> array = new ArrayList<Integer>(); 
    Scanner sc = new Scanner(System.in); 
    String userInput = ""; 
    boolean endLoop = false; 

    do{ 
    try{ 

     for (int i = 0; i < 20; i++) { 
      int element = (int)(1000.0 * Math.random()); 
      array.add(element); 
     } 
     System.out.print("\nUnsorted Array: "); 

       //Displays the unsorted ArrayList 
     for (int i = 0; i < array.size() - 1; i++) { 
      System.out.print(array.get(i) + ", "); 
     } 

     System.out.print(array.get(array.size() - 1) + "."); 
     System.out.println(); 
     bubbleSort(array); 
    } 
    catch (IndexOutOfBoundsException e) { 
     System.out.println("\nThere is an out of bounds error in the ArrayList."); 
    } 



    System.out.print("\nEnter Y to continue or N to quit: "); 
     userInput = sc.nextLine(); 

    if (userInput.equalsIgnoreCase("Y")) { 
     endLoop = false; 

    } 
    else if (userInput.equalsIgnoreCase("N")) { 
     endLoop = true; 
    } 

    else { 
     System.out.println("\nYou did not enter Y or N."); 
     System.out.println("Please try again."); 
    } 

    }while(endLoop == false); 


    } 
} 
+0

什麼是例外? – bejado

+0

您是否嘗試過使用您的調試器?或者手動跟蹤你的代碼?例如,當'i = j = 0'時需要交換條目時會發生什麼? –

+0

當i = 0且j = 0時,您得到索引= -1,即索引超出範圍。因爲你的** j **從**我**開始。 – HappyHal

回答

0

試試下面的代碼:

public static void bubbleSort(ArrayList<Integer> list){ 
     for(int i = 0; i < list.size(); i++) { 
      for(int j = 1; j < (list.size() -i); j++) { 
       if(list.get(j - 1) > list.get(j)) { 
        int temp = list.get(j-1); 
        list.set(j-1, list.get(j)); 
        list.set(j, temp); 
       }     
      } 
     }  
    } 

你做了2次失誤......先在第一個for循環的for(int j = 1; j < (list.size() -i); j++),而你寫int j=i和第二就是你沒蓋的括號{}中如果循環條件。歡呼

0

男孩你錯過了括號內的if語句。

//this is the chunk of code that I am having problems with 
       for (int j = i; j < (n - 1); j++) { 
        if (arr.get(n - 1) < arr.get(j)) {//<------here this one 
         temp = arr.get(j - 1); 
         arr.set(j - 1, arr.get(j)); 
         arr.set(j, temp); 
        }//<----this too 
       } 

只有第一個語句後,如果你不把支架考慮。

+0

它仍然不會排序......這將只會避免indexoutofbondsexception,但不會排序..在for循環其int j = 1不是j =我... – Akshay

0

您可能會誤解氣泡排序的工作原理。這裏是(從this link拍攝)現在

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest 
number to greatest number using bubble sort. In each step, elements written 
in bold are being compared. Three passes will be required. 

First Pass 
(5 1 4 2 8) (1 5 4 2 8), Here, algorithm 
compares the first two elements, and swaps since 5 > 1. 
(1 5 4 2 8) (1 4 5 2 8), Swap since 5 > 4 
(1 4 5 2 8) (1 4 2 5 8), Swap since 5 > 2 
(1 4 2 5 8) (1 4 2 5 8), Now, since these elements are already in order 
(8 > 5), algorithm does not swap them. 

Second Pass 
(1 4 2 5 8) (1 4 2 5 8) 
(1 4 2 5 8) (1 2 4 5 8), Swap since 4 > 2 
(1 2 4 5 8) (1 2 4 5 8) 
(1 2 4 5 8) (1 2 4 5 8) 
Now, the array is already sorted, but the algorithm does not know if it is 
completed. The algorithm needs one whole pass without any swap to know it is 
sorted. 

Third Pass 
(1 2 4 5 8) (1 2 4 5 8) 
(1 2 4 5 8) (1 2 4 5 8) 
(1 2 4 5 8) (1 2 4 5 8) 
(1 2 4 5 8) (1 2 4 5 8) 

的冒泡排序是如何工作的例子,比較該行想着你的代碼在這裏:

for (int i = 0; i < n; i++) { 

    //this is the chunk of code that I am having problems with 
    for (int j = i; j < (n-1); j++) { 
     if (arr.get(n-1) < arr.get(j)) 
      temp = arr.get(j-1); 
      arr.set(j-1, arr.get(j)); 
      arr.set(j, temp); 
    } 

} 

現在,如果您在本例中使用數組(5 1 4 2 8)並將其插入代碼中,最終將比較8與每個給定的數字,並且由於8已經是最大的,所以if語句將始終爲假;不會進行分類。相反,一次比較兩個相鄰的索引,並使用布爾值來指示是否發生了交換。鑑於這種功能,排序將繼續將最大數字儘可能地交換到數組的最後。所以,一旦沒有交換,就知道該數組已經排序。因此,您希望內部循環僅進入已排序的部分,不再進一步。如果您比較排序值,排序將盡快結束。嘗試使用這種思維方式並將其應用於您的代碼。