Six sorting methods of JavaScript

Source: Internet
Author: User

1. Bubble sort
The maximum value of the loop is decremented from length
Each loop can only be the last one, then decrements to the first one.

function Bubblesort () {var changeddata = new Array (); var index = 0;    Console.log ("bubbling Call");    for (var j = a.length, J >0; j--) {for        (var i = 0; i < J; i++) {            if (a[i]>a[i+1]) {                var z = 0;                z = a[i+1];                A[I+1] = A[i];                A[i] = Z;            }            Changeddata[index] = a.tostring ();            index++;        } One                }//two    showdatechange (changeddata);}

2. Select sort
Outer loop J Select Current element to Length-1
Inner loop j+1 start to length compare min min
Exchange min and A[j]

function Selectionsort () {var changeddata = new Array (); var index = 0;    Console.log ("Select call");    for (var j = 0; J < A.length-1; J + +) {        var min = a[j];        var minindex = j;        for (var i = j+1; i < a.length; i++) {            if (A[i] < min) {                min = a[i];                Minindex = i;            }        } One        A[minindex] = a[j];        A[j] = min;         Changeddata[index] = a.tostring ();        index++;    }    Showdatechange (Changeddata);

3. Insert sort (and bubble opposite)
Start with subscript 1 and choose until the end.
Every comparison between the selected and the front of him
Until he finds a smaller platoon behind him.
That's the equivalent of i+1 until the length of the line from 1 onwards.

function Insertionsort () {var changeddata = new Array (); var index = 0;    Console.log ("insert Sort");    for (var j = 1; j < A.length; J + +) {        var now = a[j];        var i = j-1;        while (i>=0 && now<a[i]) {            //Move backward array            a[i+1] = a[i];            i--;        } One        a[i+1] = now;        Changeddata[index] = a.tostring ();        index++;    }    Showdatechange (changeddata);}

4. Hill sort
Insert sort with interval change
The traditional insert sort compares the preceding adjacent objects
Hill sort compares the preceding H-object until the H-interval does not have a change
Change h until H=1

function Shellsort () {var changeddata = new Array (); var index = 0;    Console.log ("Hill Sort");    var N = a.length;    var h = 1;    if (H < N/3) {        h = h*3 + 1;//set interval    }    while (H > 0) {for        (var j = h; j < A.length; J + +) {for            (V Ar i = j;i >= h && a[i] < a[i-h]; I-= h) {                var Z;                z = a[i];                A[i] = a[i-h];                A[I-H] = Z;                 Changeddata[index] = a.tostring ();                index++;            }        }        h = (h-1)/3;//change interval    }    showdatechange (Changeddata);}

5. Merge sort
Data is divided into fractional groups with step intervals
Sort the decimal group step to be larger until it is 1/2 arrays
Two sorted arrays before and after sorting

function MergeSort (arr) {var changeddata = new Array ();    Console.log ("merge sort");    if (Arr.length < 2) {return;    } var step = 1;    var left;    var right;        while (step < arr.length) {left = 0;        right = step;            while (right + step <= arr.length) {mergearrays (arr,left,left+step,right,right+step);            left = right + step;        right = left + step;        } if (right < Arr.length) {mergearrays (arr,left,left+step,right,arr.length);    } Step *= 2; } function Mergearrays (arr,startleft,stopleft,startright,stopright) {var leftarray = new Array (Stopleft-startl        EFT + 1);        var rightarray = new Array (stopright-startright + 1);        K = Startright;            for (var i = 0; i < rightarray.length-1; i++) {rightarray[i] = arr[k];        k++;        } k = Startleft;       for (var i = 0; i < leftarray.length-1; i++) {leftarray[i] = arr[k];     k++;        } rightarray[rightarray.length-1] = Infinity;        Leftarray[leftarray.length-1] = Infinity;        var m = 0;        var n = 0; for (var k = startleft; k < stopright; k++) {if (Leftarray[m] <= rightarray[n]) {Arr[k]                = Leftarray[m];            m++;                }else{Arr[k] = rightarray[n];                n++;    }} arr = "";//Convert to String Changeddata.push (arr); } showdatechange (Changeddata);}

6. Quick Sort

Visual sorting not implemented

function QuickSort () {    Console.log ("quick sort");    function QSort (list) {        if (list.length = = 0) {            return list;        }        Select Radix        var pivotindex = Math.floor (LIST.LENGTH/2);        var pivot = list.splice (pivotindex,1) [0];        var left = [];        var right = [];        for (var i = 0; i < list.length; i++) {            if (List[i] > Pivot) {                Right.push (list[i]);            } else{                Left.push (List[i]);            }        }        Recursively replace the cardinality and sort its left and right        return qSort. Concat ([Pivot],qsort (right));    }    A = QSort (a);    Showdate ();}

Six sorting methods of JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.