JavaScript BubbleSort,如何提高其效率?

时间:2010-05-20 15:38:02

标签: javascript bubble-sort

有一个类似于此的bubblesort例程。我需要通过在数组排序时停止循环或者数组已经排序来提高效率。

function sortNumbers(listbox) {
  var x, y, holder;
  // The Bubble Sort method.
  for(x = 0; x < ranarray.length; x++) {
    for(y = 0; y < (ranarray.length-1); y++) {
      if(ranarray[y] > ranarray[y+1]) {
        holder = ranarray[y+1];
        ranarray[y+1] = ranarray[y];
        ranarray[y] = holder;
      }
    }
  }

5 个答案:

答案 0 :(得分:5)

在进入内部循环之前,创建一个布尔值来检查内部循环内是否发生了交换。当没有交换时,数组就会被排序。

function sortNumbers(listbox) { 
  var x, y, holder; 
  // The Bubble Sort method. 
  for(x = 0; x < ranarray.length; x++) { 
    var swapOccured = false;
    for(y = 0; y < (ranarray.length-1); y++) { 
      if(ranarray[y] > ranarray[y+1]) { 
        holder = ranarray[y+1]; 
        ranarray[y+1] = ranarray[y]; 
        ranarray[y] = holder; 
        swapOccured = true;
      } 
    }
    if (!swapOccured) break; 
  } 

答案 1 :(得分:2)

var a = [1, 203, 3, 746, 200];

function bubbleSort(a)
{
    var swapped;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a[i] > a[i+1]) {
                var temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                swapped = true;
            }
        }
    } while (swapped);

    for(i=0;i<a.length;i++)
    {
        document.write(a[i]+"\t");
    }
}

bubbleSort(a);

答案 2 :(得分:0)

检查内循环中是否发生交换。如果在一次运行期间没有交换,则列表将被排序。

此外,您可以使用x值来确定需要在内部循环中查看的最后一项。在x运行之后,最后的x项总是在正确的位置。

function sortNumbers(listbox) {
  var done = false;
  for (var x = 1; !done; x++) {
    done = true;
    for (var y = 0; y < ranarray.length - x; y++) {
      if (ranarray[y] > ranarray[y + 1]) {
        var holder = ranarray[y + 1];
        ranarray[y + 1] = ranarray[y];
        ranarray[y] = holder;
        done = false;
      }
    }
  }
}

答案 3 :(得分:0)

您可以使用XOR来移位数组中的位置;

var arr, len, len2;
    arr = [0, 5, 7, 8, 9, 1, 2, 3, 6, 4];

len = arr.length;

// for loops
for (var i = 0; i < len; i++) {
    for (var j = 0; j < len - 1; j++) {
        if (arr[i] <= arr[j]) {
            arr[i] = arr[i] ^ arr[j];
            arr[j] = arr[j] ^ arr[i];
            arr[i] = arr[i] ^ arr[j];
        }
    }
}

// negative while
while (--len) {
    len2 = len;
    while (len2--) {
        if (arr[len] < arr[len2]) {
            arr[len] = arr[len] ^ arr[len2];
            arr[len2] = arr[len2] ^ arr[len];
            arr[len] = arr[len] ^ arr[len2];
        }
    }
}

jsperf: http://jsperf.com/sort-tive/4

答案 4 :(得分:0)

高级气泡排序,仅需一个for循环,效率更高,更快。

 /*Advanced BUBBLE SORT with ONE PASS*/
/*Authored by :: Brooks Tare  AAU*/

public class Bubble {

    public int[] bubble(int b[]){ 
    int temp,temp1; 

    for(int i=0;i<b.length-1;i++){

            if(b[i]>b[i+1] ){
                ///swap(b[i],b[i+1]);

                temp=b[i];
                b[i]=b[i+1];
                b[i+1]=temp;

    /*Checking if there is any number(s) greater than 
      the current number. If there is swap them.*/
                while(i>0){

                    if(b[i]<b[i-1]){
                    ///swap(b[i]<b[i-1])

                        temp1=b[i];
                        b[i]=b[i-1];
                        b[i-1]=temp1;
                        i--;
                    }
                    else if(b[i]>b[i-1]){i--;}
                }
            }
            else{continue;}

        }



        return b;
    }
///the following is a function to display the Array 
        public void see(int []a){
            for(int j=0;j<a.length;j++){
                System.out.print(a[j]+",");
            }
        }



    public static void main(String []args){
        ///You can change the Array to your preference.. u can even make it dynamic 

        int b[]={5,1,4,2,0,3}; 
        int v[]=new int[100]; 
        Bubble br=new Bubble();
        v=br.bubble(b);
        br.see(v);

    }
}