Common js sorting implementation code

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Script>
Array. prototype. swap = function (I, j)
{
Var temp = this [I];
This [I] = this [j];
This [j] = temp;
}

Array. prototype. bubbleSort = function ()
{
For (var I = this. length-1; I> 0; -- I)
{
For (var j = 0; j <I; ++ j)
{
If (this [j]> this [j + 1]) this. swap (j, j + 1 );
}
}
}

Array. prototype. selectionSort = function ()
{
For (var I = 0; I <this. length; ++ I)
{
Var index = I;
For (var j = I + 1; j <this. length; ++ j)
{
If (this [j] <this [index]) index = j;
}
This. swap (I, index );
}
}

Array. prototype. insertionSort = function ()
{
For (var I = 1; I <this. length; ++ I)
{
Var j = I, value = this [I];
While (j> 0 & this [j-1]> value)
{
This [j] = this [j-1];
-- J;
}
This [j] = value;
}
}

Array. prototype. shellSort = function ()
{
For (var step = this. length> 1; step> 0; step> = 1)
{
For (var I = 0; I <step; ++ I)
{
For (var j = I + step; j <this. length; j + = step)
{
Var k = j, value = this [j];
While (k> = step & this [k-step]> value)
{
This [k] = this [k-step];
K-= step;
}
This [k] = value;
}
}
}
}

Array. prototype. quickSort = function (s, e)
{
If (s = null) s = 0;
If (e = null) e = this. length-1;
If (s> = e) return;
This. swap (s + e)> 1, e );
Var index = s-1;
For (var I = s; I <= e; ++ I)
{
If (this [I] <= this [e]) this. swap (I, ++ index );
}
This. quickSort (s, index-1 );
This. quickSort (index + 1, e );
}

Array. prototype. stackQuickSort = function ()
{
Var stack = [0, this. length-1];
While (stack. length> 0)
{
Var e = stack. pop (), s = stack. pop ();
If (s> = e) continue;
This. swap (s + e)> 1, e );
Var index = s-1;
For (var I = s; I <= e; ++ I)
{
If (this [I] <= this [e]) this. swap (I, ++ index );
}
Stack. push (s, index-1, index + 1, e );
}
}

Array. prototype. mergeSort = function (s, e, B)
{
If (s = null) s = 0;
If (e = null) e = this. length-1;
If (B = null) B = new Array (this. length );
If (s> = e) return;
Var m = (s + e)> 1;
This. mergeSort (s, m, B );
This. mergeSort (m + 1, e, B );
For (var I = s, j = s, k = m + 1; I <= e; ++ I)
{
B [I] = this [(k> e | j <= m & this [j] <this [k])? J ++: k ++];
}
For (var I = s; I <= e; ++ I) this [I] = B [I];
}

Array. prototype. heapSort = function ()
{
For (var I = 1; I <this. length; ++ I)
{
For (var j = I, k = (j-1)> 1; k> = 0; j = k, k = (k-1)> 1)
{
If (this [k]> = this [j]) break;
This. swap (j, k );
}
}
For (var I = this. length-1; I> 0; -- I)
{
This. swap (0, I );
For (var j = 0, k = (j + 1) <1; k <= I; j = k, k = (k + 1) <1)
{
If (k = I | this [k] <this [k-1]) -- k;
If (this [k] <= this [j]) break;
This. swap (j, k );
}
}
}

Function generate ()
{
Var max = parseInt (txtMax. value), count = parseInt (txtCount. value );
If (isNaN (max) | isNaN (count ))
{
Alert ("the number and maximum value must be an integer ");
Return;
}
Var array = [];
For (var I = 0; I <count; ++ I) array. push (Math. round (Math. random () * max ));
TxtInput. value = array. join ("\ n ");
TxtOutput. value = "";
}

Function demo (type)
{
Var array = txtInput. value = ""? []: TxtInput. value. replace (). split ("\ n ");
For (var I = 0; I <array. length; ++ I) array [I] = parseInt (array [I]);
Var t1 = new Date ();
Eval ("array." + type + "Sort ()");
Var t2 = new Date ();
LblTime. innerText = t2.valueOf ()-t1.valueOf ();
TxtOutput. value = array. join ("\ n ");
}
</Script>

<Body onload = generate ()>
<Table style = "width: 100%; height: 100%; font-size: 12px; font-family: ">
<Tr>
<Td align = right>
<Textarea id = txtInput readonly style = "width: 100px; height: 100%"> </textarea>
</Td>
<Td width = 150 align = center>
Number of random numbers <input id = txtCount value = 500 style = "width: 50px"> <br>
Maximum random number <input id = txtMax value = 1000 style = "width: 50px"> <br>
<Button onclick = generate ()> regenerate </button> <br>
Time elapsed (MS): <label id = lblTime> </label> <br>
<Button onclick = demo ("bubble")> bubble sort </button> <br>
<Button onclick = demo ("selection")> select sort </button> <br>
<Button onclick = demo ("insertion")> insert sort </button> <br>
<Button onclick = demo ("shell")> shell sorting </button> <br>
<Button onclick = demo ("quick")> quick sorting (recursion) </button> <br>
<Button onclick = demo ("stackQuick")> quick sorting (stack) </button> <br>
<Button onclick = demo ("merge")> merge and sort </button> <br>
<Button onclick = demo ("heap")> heap sorting </button> <br>
</Td>
<Td align = left>
<Textarea id = txtOutput readonly style = "width: 100px; height: 100%"> </textarea>
</Td>
</Tr>
</Table>
</Body>

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.