2012-09-09 120 views
-4

我想寫一個泡泡排序程序。這表明error.but我不明白,爲什麼?我在Java泡沫排序在JAVA

public static void main(String[] args) { 
    int []arr={12,23,43,34,3,6,7,1,9,6}; 
     { 
       int temp; 
       for (int i=0;i<arr.length;i++) 
       { 
       for (int j=0;j<arr.length-i;j++) 
       { 
        if (arr[j]>arr[j+1]) 
       { 
        temp=arr[j]; 
        arr[j+1]=arr[j]; 
        arr[j+1]=temp; 
        } 
       } 
       } 
      } 
     for(int i=0; i<arr.length; i++) 
     { 
      System.out.print(arr[i] + " "); 
     } 
    } 
+6

嗨華碩HP!你願意和我們分享錯誤信息嗎?你知道,我們無法讀懂你的想法。 – Adam

+0

另外,這功課嗎?如果是這樣,請標記爲家庭作業。 – Michael

+0

錯誤::線程「main」中的異常java.lang.ArrayIndexOutOfBoundsException:10 \t at SortExperiment.main(SortExperiment.java:33)Line 33:if(arr [j]> arr [j + 1]) –

回答

1

試試這個新的代碼......

public class TestBubbleSort { 
    public static void main(String[] args) { 
     int unsortedArray[] = {10, 97, 6, 23, 0, -45, 697, -1000, 1, 0}; //Random set of numbers for example. 
     int i; 

     bubbleSort(unsortedArray, unsortedArray.length); //Pass the array to be sorted and its length. 

     System.out.println("After sorting, the list elements are: "); //Just to show you it worked. :) 

     for(i=0; i<unsortedArray.length; i++) { 
      System.out.print(unsortedArray[i] + " "); 
     } 
    } 

    private static void bubbleSort(int[] unsortedArray, int length) { 
     int temp, counter, index; 

     for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array. 
      for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter. 
       if(unsortedArray[index] > unsortedArray[index+1]) { //Test if need a swap or not. 
        temp = unsortedArray[index]; //These three lines just swap the two elements: 
        unsortedArray[index] = unsortedArray[index+1]; 
        unsortedArray[index+1] = temp; 
       } 
      } 
     } 
    } 
} 
+1

@Alohci功課標籤以後增加了.......而且我不值得+1,然後-2我正確的答案......太有解釋......所以,OP可以更好地理解它 –

+0

@謝謝.............................. –

+0

感謝所有............... ....... –

3

有什麼錯誤?

我懷疑這是IndexOutOfBoundsException?這也許是if聲明在您使用j + 1,當j == arr.length - 1i == 0

由於它的功課,我會離開它你來,雖然修復它。

0

請試試這個代碼,它是按相反的順序,但你會得到的想法。

public class SortArray 
{ 
    public static void main(String[] args) 
    { 
     int[] arr={4,6,4,2,764,23,23}; 
     sort(arr); 
    } 
    static void sort(int[] arr) 
    { 
     int k; 
     for(int i=0;i<arr.length;i++) 
     { 
      for(int j=i;j<arr.length-1;j++) 
       { 
        if(arr[i]<arr[j+1]) 
        { 
         k=arr[j+1]; 
         arr[j+1]=arr[i]; 
         arr[i]=k; 
        } 
       } 
      System.out.print(arr[i]+" "); 
     } 
    } 
} 
0
public static void main(String[] args) { 
     //insert random value in array 
     Scanner sc=new Scanner(System.in); 
     System.out.println("no of element"); 
     int noEle=sc.nextInt(); 
     int[] eleArr=new int[noEle] ;//storing element in this array 
     for(int i=0;i<noEle;i++) 
     { 
      eleArr[i]=sc.nextInt();//enter element for storing 
     } 
     for(int i=0;i<eleArr.length;i++) 
     { 
      for(int j=0;j<eleArr.length-1;j++) 
     { 
      if(eleArr[j]>eleArr[j+1]) 
      {//nothing but swaping logic without taking third variable 
       eleArr[j]=eleArr[j]+eleArr[j+1]; 
       eleArr[j+1]=eleArr[j]-eleArr[j+1]; 
       eleArr[j]=eleArr[j]-eleArr[j+1]; 
      } 
     } 
     } 
     //getting sorted elemen as bubblesort 
     for(int i=0;i<noEle;i++) 
     { 
      System.out.print(eleArr[i]+" "); 
     } 
     System.out.println(); 
    } 
+0

試試這個代碼肯定它會爲冒泡排序工作我 – pankaj

-1

您可以在此查看代碼:

public static void BubbleSort(int[] Array){ 

    for(int i = Array.length ; i>0 ; i--) 
    { 
      for(int j=0;j<i-1;j++){ 

       if(Array[j]>Array[j+1]) 
        Swap(Array,j,j+1); 
      } 
    } 

} 
+0

認爲你應該解釋你的代碼 –