There is another way to optimize this bubble-sort?

ぃ、小莉子 提交于 2019-12-08 10:15:42

问题


I have this code which sort a numeric array using bubble sort algorithm.

var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];

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

bubbleSort(a);
alert(a);

You can see it here: http://jsfiddle.net/x7VpJ/

Well, it works perfect as you can check, but I wondered if there is another way to optimize this to do fewer loops using the actual method. I mean, using swapped variable. Maybe omiting the sorted items... some idea?

Thanks in advance.


回答1:


Here is a list of different sorting algorithms in JS. I made a page that compares sorting times and from what I noticed "Quick" seems the best option.

Bubble Sort:

var arrBubble= new Array();
var tmpBubble;
function srtBubble(){
    tmpDate=new Date();
    timBegin=tmpDate.getTime();

    var blnBubbleSwitch;
    do {
        blnBubbleSwitch=false;
        for(var i=0;i<arrBubble.length-1;i++){
            if(arrBubble[i]>arrBubble[i+1])
            {
                tmpBubble=arrBubble[i];
                arrBubble[i]=arrBubble[i+1];
                arrBubble[i+1]=tmpBubble;
                blnBubbleSwitch=true;
            }
        }
    } while(blnBubbleSwitch);
}

Insertion:

var arrinsertion= new Array();
var tmpinsertion;
function srtinsertionion(){
    var j;
    blninsertionSwitch=false;
    for(var i=0;i<arrinsertion.length;i++){
        tmpinsertion=arrinsertion[i];
        j=i;
        while((j>=0)&&arrinsertion[j-1]>tmpinsertion)
        {
            arrinsertion[j]=arrinsertion[j-1];
            j--;
            //blninsertionSwitch=true;
        }
        arrinsertion[j]=tmpinsertion;
    }
}

Shell:

function srtShell(){
var arrShell= new Array();
    for (var h = arrShell.length; h = parseInt(h / 2);) {
        for (var i = h; i < arrShell.length; i++) {
            var k = arrShell[i];
            for (var j = i; j >= h && k < arrShell[j - h]; j -= h)
                arrShell[j] = arrShell[j - h];
            arrShell[j] = k;
        }
    }
}

Quick:

function srtQuick()
{
    var arrQuick= new Array();
    qsort(arrQuick, 0, arrQuick.length);
}
function qsort(array, begin, end)
{
    if(end-1>begin) {
        var pivot=begin+Math.floor(Math.random()*(end-begin));

        pivot=partition(array, begin, end, pivot);

        qsort(array, begin, pivot);
        qsort(array, pivot+1, end);
    }
}
Array.prototype.swap=function(a, b)
{
    var tmp=this[a];
    this[a]=this[b];
    this[b]=tmp;
}
function partition(array, begin, end, pivot)
{
    var piv=array[pivot];
    array.swap(pivot, end-1);
    var store=begin;
    var ix;
    for(ix=begin; ix<end-1; ++ix) {
        if(array[ix]<=piv) {
            array.swap(store, ix);
            ++store;
        }
    }
    array.swap(end-1, store);

    return store;
}

Merge:

function merge_inplace(array, begin, begin_right, end)
{
    var arrMerge= new Array();
    for(;begin<begin_right; ++begin) {
        if(array[begin]>array[begin_right]) {
            var v=array[begin];
            array[begin]=array[begin_right];
            insertion(array, begin_right, end, v);
        }
    }
}

function insertion(array, begin, end, v)
{
    while(begin+1<end && array[begin+1]<v) {
        array.swap(begin, begin+1);
        ++begin;
    }
    array[begin]=v;
}

function msort(array, begin, end)
{
    var size=end-begin;
    if(size<2) return;

    var begin_right=begin+Math.floor(size/2);

    msort(array, begin, begin_right);
    msort(array, begin_right, end);
    merge_inplace(array, begin, begin_right, end);
}

function srtMerge()
{
    msort(arrMerge, 0, arrMerge.length);
}

Built in JS Sort:

function srtJSSort()
{
    var arrJSSort= new Array();
    arrJSSort.sort(function(a,b){return a-b});
}

Of course I would suggest using your arrays outside of the function so you can continue using it after the data is sorted.




回答2:


If I stick with the original question (and I will not suggest completely different algorithms...).

Yes, there is another improvement - bi-direction bubble sort, called shaker sort. It eliminates problem of turtles and rabbits. In one-directional bubble sort light bubbles are moving fastly towards the end of the array (rabbits), while the heavy bubbles are moving towards the start very slowly. But if you sort in bi-directional manner, both types of bubbles are sorted pretty fast (still in O(n^2), but usually faster than with the conventional bubble sort).




回答3:


Here is the solution that I was looking for:

var a = [34, 203, 3, 746, 200, 984, 198, 764, 9, 1, 32423, 3455, 23, 4234,23];

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

bubbleSort(a);
alert(a);

http://jsfiddle.net/x7VpJ/1/

Thanks all responses.



来源:https://stackoverflow.com/questions/15956018/there-is-another-way-to-optimize-this-bubble-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!