java JAVA中的冒泡排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12339939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:29:05  来源:igfitidea点击:

Bubble Sort in JAVA

java

提问by Asus HP

I am trying to write a Bubble sort program. It shows error.but I don't understand why?I am new in 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] + " ");
         }
    }

回答by pickypg

What's the error?

有什么错误?

I suspect that it's IndexOutOfBoundsException? It's probably the ifstatement where you use j + 1, when j == arr.length - 1when i == 0.

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

As it's homework, I'll leave it up to you to fix it though.

因为这是家庭作业,所以我会让你来解决它。

回答by Kumar Vivek Mitra

Try this code......

试试这个代码......

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

回答by Sandeep

Please Try this code , It is in reverse order but you will get the idea .

请试试这个代码,它的顺序是相反的,但你会明白的。

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

回答by pankaj

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

回答by Nguyen Viet Quan

You can view across this code:

您可以查看此代码:

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

}