2017-02-16 18 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のときは、インデックスの範囲外のインデックスを取得します。あなたの** j **は** i **から始まるからです。 – 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(int j = 1; j < (list.size() -i); j++)あなたがint j=iを書いている間、あなたはに{}括弧をカバーしていない第二ifループステートメント.Cheers

0

Boyは、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 not j = i ... – Akshay

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); 
    } 

} 

:ここ

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) 

今(this linkから取られた)バブルソートがどのように機能するかの例があり、ここにあなたのコードに思考のラインを比較(5 1 4 2 8)、あなたのコードにプラグインすれば、8を各数値に比較することになります。そして、8が既に最大であるため、if文は常にfalseになります。ソートは行われません。代わりに、一度に2つの隣接するインデックスを比較し、ブール値を使用してスワップが発生したかどうかを示します。この種の機能性を考えれば、ソートは可能な限りアレイの最後まで最大の番号をスワップし続けます。だから、一度スワップがなければ、配列がソートされていることを知っています。したがって、内側のループは既にソートされた部分にのみ移動し、それ以上は必要ありません。ソートされた値を比較すると、ソートは早く終了します。この考え方を試して、コードに適用してください。