Several sorting methods of JavaScript

Source: Internet
Author: User
Tags benchmark

Sorting is to sort records in files in ascending (or descending) Order of keywords. The exact definition is as follows:

Input: N records R1, R2 ,..., RN, whose corresponding keywords are K1, K2 ,..., KN.

Output: RIL, ri2 ,..., So that ki1 is less than or equal to ki2... ≤Kin. (Or ki1 ≥ ki2 ≥... ≥ Kin ).

Here, we will briefly introduce several sorting methods, such as direct insertion sorting, hier sorting, Bubble sorting, quick sorting, and direct selection sorting.CodePassed the test under IE6.

Basic Idea of direct insertion and sorting

Assume that the records to be sorted are stored in the array R [1. N. In the initial phase, R [1] is a self-contained ordered zone, and the unordered zone is R [2. N]. From I = 2 until I = N, insert R [I] into the current ordered zone R [1 .. I-1] sequentially to generate an ordered zone containing N records.

algorithm description
function insertsort (ARR) {// insert sort-> direct insert sort
var ST = new date ();
var temp, J;
for (VAR I = 1; I If (ARR [I]) <(ARR [I-1]) {
temp = arr [I];
J = I-1;
do {
arr [J + 1] = arr [J];
j --;

}< br>
while (j>-1 & (temp) <(ARR [J]);
arr [J + 1] = temp;

}// endif

}< br>
Status = (new date ()-St) + 'ms ';
return arr;

}

Basic Thinking of hill sorting

Take an integer D1 smaller than N as the first increment, and divide all records of the file into D1 groups. All records whose distance is a multiple of DL are placed in the same group. Sort directly inserted persons in each group first, and then take the second incremental D2 <d1 repeat the preceding grouping and sorting, until the incremental dt = 1 (dt <DT-L <... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting.

This method is essentially a grouping insertion method.

Algorithm Description

Function shellsort (ARR) {// insert sort-> hier sort

VaR ST = new date ();

VaR increment = arr. length;

Do {

Increment = (increment/3 | 0) + 1;

Arr = shellpass (ARR, increment );

}

While (increment> 1)

Status = (new date ()-St) + 'ms ';

Return arr;

}

Function shellpass (ARR, d) {// hier sorting segmentation execution function

VaR temp, J;

For (VAR I = D; I <arr. length; I ++ ){

If (ARR [I]) <(ARR [I-d]) {

Temp = arr [I]; j = I-d;

Do {

Arr [J + D] = arr [J];

J = J-D;

}

While (j>-1 & (temp) <(ARR [J]);

Arr [J + D] = temp;

} // Endif

}

Return arr;

}

Function shellsort (ARR) {// insert sort-> hier sort

VaR ST = new date ();

VaR increment = arr. length;

Do {

Increment = (increment/3 | 0) + 1;

Arr = shellpass (ARR, increment );

}

While (increment> 1)

Status = (new date ()-St) + 'ms ';

Return arr;

}

Function shellpass (ARR, d) {// hier sorting segmentation execution function

VaR temp, J;

For (VAR I = D; I <arr. length; I ++ ){

If (ARR [I]) <(ARR [I-d]) {

Temp = arr [I]; j = I-d;

Do {

Arr [J + D] = arr [J];

J = J-D;

}

While (j>-1 & (temp) <(ARR [J]);

Arr [J + D] = temp;

} // Endif

}

Return arr;

}

Basic Idea of Bubble Sorting

The sorted record array R [1. N] is vertically arranged. Each record R [I] is considered as a bubble with the weight of R [I]. Key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.

Algorithm Description

Function bubblesort (ARR) {// exchange sort-> bubble sort

VaR ST = new date ();

VaR temp;

VaR exchange;

For (VAR I = 0; I <arr. length; I ++ ){

Exchange = false;

For (var j = arr. Length-2; j> = I; j --){

If (ARR [J + 1]) <(ARR [J]) {

Temp = arr [J + 1];

Arr [J + 1] = arr [J];

Arr [J] = temp;

Exchange = true;

}

}

If (! Exchange) break;

}

Status = (new date ()-St) + 'ms ';

Return arr;

}

Basic Idea of quick sorting

The original problem is divided into several subproblems with smaller sizes but similar structures as the original problem. Recursively solve these subproblems, and then combine the solutions of these subproblems into the solutions of the original problem.

In R [low .. select a record in high] as the benchmark to divide the current disordered zoning into two smaller subintervals (left and right) R [low .. pivotpos-1) and R [pivotpos + 1 .. high], and make the keywords of all records in the left subinterval less than or equal to the benchmark record (may be recorded as the benchmark) keyword. key, the key of all records in the subinterval on the right is greater than or equal to limit. the benchmark record is located at the correct position (pivotpos), and does not need to be sorted in the future.

Algorithm Description

Function quicksort (ARR) {// exchange sorting-> quick sorting

If (arguments. length> 1 ){

VaR low = arguments [1];

VaR high = arguments [2];

} Else {

VaR low = 0;

VaR high = arr. Length-1;

}

If (low
// Function partition

VaR I = low;

VaR J = high;

VaR outputs = arr [I];

While (I <j ){

While (I <J & arr [J]> = callback)

J --;

If (I <j)

Arr [I ++] = arr [J];

While (I <J & arr [I] <= random)

I ++;

If (I <j)

Arr [j --] = arr [I];

} // Endwhile

Arr [I] = role;

// End Function

VaR partition TPOs = I; // partition (ARR, low, high );

Quicksort (ARR, low, pivotpos-1 );

Quicksort (ARR, pivotpos + 1, high );

} Else

Return;

Return arr;

}

Directly select the basic idea of sorting

The Direct selection and sorting of files with N records can be directly selected and sorted through n-1 rows to get the ordered results:

① Initial status: the disordered area is R [1. N], and the ordered area is empty.

② Sorting by 1st bits

In the unordered zone R [1 .. n] selects the record R [k] with the minimum keyword, swaps it with the 1st records R [1] In the unordered area, so that R [1 .. 1] and R [2 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count.

......

③ Sort by I

At the beginning of the I-th sorting, the current ordered and disordered areas are R [1 .. I-1] and R [I. N] (1 ≤ I ≤ N-1), respectively ). This sort field selects the record R [k] with the smallest keyword from the current unordered area and exchanges it with the 1st records R [I] In the unordered area, so that R [1 .. i] and R [I + 1 .. n] into a new ordered area with one more record count and a new unordered area with one fewer record count.

In this way, the direct sorting of files with N records can be directly selected through n-1 to obtain the ordered results.

Algorithm Description

Function selectsort (ARR) {// select sort-> select sort directly

VaR ST = new date ();

VaR temp;

For (VAR I = 0; I <arr. length; I ++ ){

VaR K = I;

For (var j = I + 1; j <arr. length; j ++ ){

If (ARR [J]) <(ARR [k])

K = J;

}

If (K! = I ){

Temp = arr [I];

Arr [I] = arr [k];

Arr [k] = temp;

}

}

Status = (new date ()-St) + 'ms ';

Return arr;

}

The Code is as follows:


<Style> <br/> fieldset {<br/> font-size: 12px; <br/> padding: 10px; <br/> width: 80%; <br/> margin: auto; <br/>}< br/> input {<br/> font-size: 12px; <br/> font-family: tahoma; <br/>}< br/> </style> <br/> <title> sort </title> <br/> <H3 align = "center"> sort </ h3> <br/> <fieldset> <br/> <legend> insert sort </legend> </P> <p> <B> insert sort directly </ b> <br/> enter a string of characters to be sorted, separated by commas <br/> <input name = Insert Type = text size = 100 value = "g, V, U, F, P, o, I, A, T, J, E, L, K "> <br/> <br> <input type = button value =" sort "onclick =" alert (insertsort (insert. value. split (','))); "> </P> <p> <B> hier sorting </B> <br/> <input name = shell type = text size = 100 value = "G, v, U, F, P, o, I, A, T, J "> <br/> <br> <input type = button value =" sort "onclick =" alert (shellsort (shell. value. split (','))); "> </P> <p> </fieldset> <br/> <p> <br/> <fieldset> <br/> <legend> exchange sorting </legend> </P> <p> <B> Bubble Sorting </B> <br/> <I Nput name = bubble type = text size = 100 value = "g, V, U, F, P, o, I, A, T, J, E, l, K "> <br/> <br> <input type = button value =" sort "onclick =" alert (bubblesort (bubble. value. split (','))); "> </P> <p> <B> quick sorting <br> <br/> </B> <br/> <input name = Quick type = text size = 100 value = "3, 1, 5, 4, 6 "> <br/> <br> <input type = button value =" sort "onclick =" alert (quicksortdemo (quick. value. split (','); "> </P> <p> </fieldset> <br/> <p> <br/> <fieldset> <Br/> <legend> select sort </legend> </P> <p> <B> select sort directly </B> <br/> <input name = select1 type = text size = 100 value = "G, v, U, F, P, o, I, A, T, J, E, l, K "> <br/> <br> <input type = button value =" sort "onclick =" alert (selectsort (select1.value. split (','); "> </P> <p> ...... </P> <p> </fieldset> </P> <p> <SCRIPT> <br/> function insertsort (ARR) {// insert sort-> direct insert sort <br/> var ST = new date (); <br/> var temp, J; <br/> for (VAR I = 1; I <arr. le Ngth; I ++) {<br/> If (ARR [I]) <(ARR [I-1]) {<br/> temp = arr [I]; <br/> J = I-1; <br/> do {<br/> arr [J + 1] = arr [J]; <br/> j --; <br/>}< br/> while (j>-1 & (temp) <(ARR [J]); <br/> arr [J + 1] = temp; <br/>}// endif <br/>}< br/> Status = (new date ()-St) + 'ms'; <br/> return arr; <br/>}</P> <p> function shellsort (ARR) {// insert sorting-> hier sorting <br/> var ST = new date (); <br/> var increment = arr. length; <Br/> do {<br/> increment = (increment/3 | 0) + 1; <br/> arr = shellpass (ARR, increment ); <br/>}< br/> while (increment> 1) </P> <p> Status = (new date ()-St) + 'ms '; <br/> return arr; <br/>}< br/> function shellpass (ARR, d) {// hier sorting segmentation execution function <br/> var temp, J; <br/> for (VAR I = D; I <arr. length; I ++) {<br/> If (ARR [I]) <(ARR [I-d]) {<br/> temp = arr [I]; j = I-d; <br/> do {<br/> arr [J + D] = arr [J]; <Br/> J = J-D; <br/>}< br/> while (j>-1 & (temp) <(ARR [J]); <br/> arr [J + D] = temp; <br/>}// endif <br/>}< br/> return arr; <br/>}</P> <p> function bubblesort (ARR) {// exchange sorting-> Bubble Sorting <br/> var ST = new date (); <br/> var temp; <br/> var exchange; <br/> for (VAR I = 0; I <arr. length; I ++) {<br/> exchange = false; <br/> for (VAR J = arr. length-2; j> = I; j --) {<br/> If (ARR [J + 1]) <(ARR [J]) {<br/> temp = Arr [J + 1]; <br/> arr [J + 1] = arr [J]; <br/> arr [J] = temp; <br/> exchange = true; <br/>}< br/> If (! Exchange) break; <br/>}< br/> Status = (new date ()-St) + 'ms'; <br/> return arr; <br/>}</P> <p> function quicksortdemo (ARR) {<br/> var ST = new date (); <br/> var result = quicksort (ARR); <br/> Status = (new date ()-St) + 'ms'; <br/> return result; <br/>}</P> <p> function quicksort (ARR) {// exchange sorting-> fast sorting <br/> If (arguments. length> 1) {<br/> var low = arguments [1]; <br/> var high = arguments [2]; <Br/>} else {<br/> var low = 0; <br/> var high = arr. length-1; <br/>}< br/> If (low <pigh) {<br/> // function partition <br/> var I = low; <br/> var J = high; <br/> var callback = arr [I]; <br/> while (I <j) {<br/> while (I <J & arr [J]> = running) <br/> j --; <br/> if (I <j) <br/> arr [I ++] = arr [J]; <br/> while (I <J & arr [I] <= random) <br/> I ++; <br/> if (I <j) <br/> arr [j --] = arr [I]; <br/>}// endwhile <br/> Arr [I] = callback; <br/> // end function <br/> var javastpos = I; // partition (ARR, low, high ); <br/> quicksort (ARR, low, pivotpos-1); <br/> quicksort (ARR, pivotpos + 1, high); <br/>}else <br/> return; <br/> return arr; <br/>}</P> <p>/* function partition (ARR, I, j) {// fast sorting, sort the sorted array <br/> var placement = arr [I]; <br/> while (I <j) {<br/> while (ARR [J]> = running) <br/> j --; <br/> if (I <j) <br/> arr [I ++] = arr [J]; <br/> while (ARR [I] <= random) <br/> I ++; <br/> if (I <j) <br/> arr [j --] = arr [I]; <br/>}< br/> arr [I] = Beijing; <br/> return arr; <br/>}*/</P> <p> function selectsort (ARR) {// select sorting-> directly select sorting <br/> var ST = new date (); <br/> var temp; <br/> for (VAR I = 0; I <arr. length; I ++) {<br/> var K = I; <br/> for (var j = I + 1; j <arr. length; j ++) {<br/> If (ARR [J]) <(ARR [k]) <br/> K = J; <br/>}< br/> If (K! = I) {<br/> temp = arr [I]; <br/> arr [I] = arr [k]; <br/> arr [k] = temp; <br/>}< br/> Status = (new date ()-St) + 'ms'; <br/> return arr; <br/>}</P> <p> function Unicode (STR) {// evaluate the Unicode code of a string <br/> var uni = 0; <br/> for (VAR I = 0; I <Str. length; I ++) {<br/> uni + = Str. charcodeat (I)/6553.5 * Math. pow (10, str. length-I); <br/>}< br/> return uni; <br/>}< br/> </SCRIPT>
[Ctrl + A select all prompt: you can modify part of the code first, and then press run code]

From: http://www.blueidea.com/tech/program/2004/2344.asp

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.