-5

我正在尝试编写一个冒泡排序程序。它显示错误。但我不明白为什么?我是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] + " ");
         }
    }
4

5 回答 5

3

有什么错误?

我怀疑是IndexOutOfBoundsException?这可能是您使用的语句ifwhere j + 1when j == arr.length - 1i == 0

由于这是家庭作业,我会留给你来解决它。

于 2012-09-09T14:33:32.877 回答
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;
                }
            }
        }
    }
}
于 2012-09-09T14:32:08.703 回答
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();
    }
于 2014-02-26T06:39:56.797 回答
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]+" ");
        }   
    }
}
于 2013-09-11T14:25:33.050 回答
-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);
            }
    }

}
于 2015-06-22T04:08:00.307 回答