JAVA中的冒泡排序

时间:2012-09-09 14:29:22

标签: 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] + " ");
         }
    }

5 个答案:

答案 0 :(得分:3)

错误是什么?

我怀疑它是IndexOutOfBoundsException?在if j + 1j == arr.length - 1,您可能会使用i == 0语句{{1}}。

因为它是家庭作业,我会留给你修理它。

答案 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;
                }
            }
        }
    }
}

答案 2 :(得分: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]+" ");
        }   
    }
}

答案 3 :(得分: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();
    }

答案 4 :(得分:-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);
            }
    }

}