冒泡排序算法实现

时间:2013-02-18 11:06:54

标签: java algorithm sorting bubble-sort

我正在学习冒泡排序算法,我遇到了两种实现方法,所以我的问题是哪个更好,为什么?

第一

for(k=0;k<array.length-1;k++){ 
            if(array[k] > array[k+1]){
                int temp = array[k];
                array[k] = array[k+1];
                array[k+1] = temp;
            }
        }

第二

for(i=array.length-1;i>=0;i--){
        for(k=0;k<i;k++){ 
            if(array[k] > array[k+1]){
                int temp = array[k];
                array[k] = array[k+1];
                array[k+1] = temp;
            }
        }
        }

6 个答案:

答案 0 :(得分:1)

关于哪一个更好 - 首先是关于哪一个对阵列进行排序,答案是第二个

检查this以获得更多想法

答案 1 :(得分:0)

你的实现的第一个没有正确排序因为算法只能切换2个相邻的元素。

实施例。

输入数据{6,5,4,3,2}

第一次迭代使这个 - &gt; {5,6,4,3,2}和现在5位于第一个位置并且没有机会改变这个位置,因为循环索引(k)是1.并且进一步迭代它增加..

冒泡排序总是需要2个循环

public static void bubbleSort(int[] array){
    for (int i = 0; i < array.length - 1; i++) {
        for (int j = 0; j < array.length - i - 1; j++) {
            if(array[j] < array[j+1]){
                int tmp = array[j];
                array[j] = array[j+1];
                array[j+1] = tmp;
            }
        }
    }
}  

答案 2 :(得分:0)

我发现维基百科(http://en.wikipedia.org/wiki/Bubble_sort

中效率更高
procedure bubbleSort( A : list of sortable items )
    n = length(A)
    repeat
       newn = 0
       for i = 1 to n-1 inclusive do
          if A[i-1] > A[i] then
             swap(A[i-1], A[i])
             newn = i
           end if
       end for
       n = newn
    until n = 0
end procedure

答案 3 :(得分:0)

第二个将对数组进行排序。但是,只有在最后一次迭代中至少有一次交换时,才能通过运行来减少第二次循环中的比较。

答案 4 :(得分:0)

你的第一个解决方案不起作用,它不会对数组进行排序。但第二个,将工作,它将从最小到最大的数据排序。有关更多说明,请参阅以下内容:

嗯,冒泡排序算法背后的想法是在比较每对相邻项目的同时浏览数据的数组/集合,如果它们的顺序错误则交换它们。重复遍历数组/列表,直到不需要交换,这表示列表/数组已排序。 时间复杂度:O(n ^ 2)。以及我们将使用原始数组的空间。让我们使用下面的数组说明上面的段落:

    //array of integer to be sorted
    int[] arrayToSort=new int[]{1,7,81,2,-2,9,9,6,-6};
    //repeat until we're done sorting 
    while (true){
        //flag to check if we did swap number(s)
        boolean didSort=false;
        /*
         *  this inner loop is being used to find numbers that need to be    
         *  swapped and then help in swapping them
         */
        for(int count=0;count<arrayToSort.length-1;count++){
            //check if we need to swap
            if(arrayToSort[count]>arrayToSort[count+1]){
                int temp=arrayToSort[count+1];
                arrayToSort[count+1]=arrayToSort[count];
                arrayToSort[count]=temp;
                //set our swap flag so that we will know that we did swap
                didSort=true;
            }

        }

        //check we did a swap in our last inner loop iteration if not will   
        //be done sorting, then break the outer loop
        if(!didSort){
            break;
        }   
    }

    //let's print the sorted array.
    for(int i=0;i<arrayToSort.length;i++){
        System.out.print(arrayToSort[i]+", ");
    }

答案 5 :(得分:-2)

请勿使用bubblesort(请改用插入排序)。

编辑:

由于人们不断对此进行投票,请阅读http://warp.povusers.org/grrr/bubblesort_misconceptions.html

Bubblesort是帕累托无效的,因为它比例如insertionsort和selectionsort更慢且更难理解。只是不要使用(也不要教)Bubblesort。

相关问题