Several sorting methods of JavaScript

Source: Internet
Author: User
Tags date array execution insert key key words return sort
Javascript| sort

The so-called sort, is to organize the records in the file, so that the key words in ascending (or descending) order. The exact definition is as follows:
Input: N Records R1,r2,...,rN, the corresponding key words are K1,k2,...,k n respectively.
Output: Ril,ri2,...,rin, making Ki1≤ki2≤ ... ≤k in. (or Ki1≥ki2≥ ...) ≥k in).

Here, we introduce a few sorts of sorting methods, direct insertion sort, Ashley sort, bubble sort, quick sort, direct selection sort, the code mentioned in this article is tested by IE6.

Direct Insertion Sort Basic idea
Suppose the records to be sorted are stored in array R[1..N]. Initially, the r[1] is a 1 ordered region and the disordered region is R[2..N]. From i=2 up to I=n, the r[i] is inserted into the current ordered region r[1..i-1, in order to generate an ordered region containing N records.

Algorithm description

function Insertsort (arr) {//Insertion sort-> Direct insertion method
var st = new Date ();
var temp, J;
for (var i=1; i<arr.length; i++) {
if ((Arr[i]) < (Arr[i-1]) {
temp = Arr[i];
j = i-1;
do {
ARR[J+1] = Arr[j];
j--;
}
while (j>-1 && (temp) < (ARR[J));
ARR[J+1] = temp;
}//endif
}
Status = (new Date ()-st) + ' MS ';
return arr;
}

The basic idea of hill sorting
First, take a whole number of D 1 less than nas the first increment, and divide all the records into D1 groups. All records with a multiple of the distance of DL are placed in the same group. First in each group of direct interpolation sort; Then, take the second increment d2<d1 to repeat the grouping and sorting until the increment Dt=1 (dt<dt-l<...<d2<d1 , where all records are placed in the same group for direct insertion to sort.
The method is essentially a grouping insertion method.

Algorithm description

function Shellsort (arr) {//insert sort-> Ashley 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) {//child sort segmented execution functions
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;
}

Bubble Sort Basic idea
The sorted array of records R[1..N] is arranged vertically, and each record r[i] is considered to be a bubble with a weight of r[i].key. According to the principle that light bubbles cannot be under a heavy bubble, scan the array r from bottom to top "float" the light bubbles that violate this principle. So repeatedly, until the end of any two bubbles are light on the top, the heavy in the next.

Algorithm description

function Bubblesort (arr) {//Interchange 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;
}

The basic idea of fast sorting
The original problem is decomposed into a number of smaller but structurally similar to the original problem. Recursively solve these child problems, and then combine the solutions of these child problems into the solution of the original problem.
In R[low. High] Select a record as the benchmark (Pivot), which divides the current unordered division into the left and right two smaller sub ranges r[low ... PIVOTPOS-1) and R[pivotpos+1..high], and make the key for all records in the left child interval less than or equal to the datum record (may be recorded as pivot) Keyword Pivot.key, the key for all records in the right child interval is greater than or equal to Pivot.key, while the Datum record pivot is located on the correct location (Pivotpos) and does not need to participate in subsequent sorting.

Algorithm description

function QuickSort (arr) {//Exchange sort-> Quick Sort
if (arguments.length>1) {
var low = arguments[1];
var high = arguments[2];
} else {
var low = 0;
var high = arr.length-1;
}
if (Low < high) {
function Partition
var i = low;
var j = high;
var pivot = Arr[i];
while (I&LT;J) {
while (I<j && arr[j]>=pivot)
j--;
if (I&LT;J)
arr[i++] = Arr[j];
while (I<j && arr[i]<=pivot)
i++;
if (I&LT;J)
arr[j--] = Arr[i];
}//endwhile
Arr[i] = pivot;
End Function
var pivotpos = i; Partition (Arr,low,high);
QuickSort (arr, Low, pivotpos-1);
QuickSort (arr, pivotpos+1, high);
} else
Return
return arr;
}

The basic idea of direct selection sorting
N Records of the direct selection of files can be ordered through the N-1 direct selection order results:
① Initial state: Unordered area is R[1..N], ordered area is empty.
② 1th Trip Order
In the unordered region R[1..N], the record of the smallest key word is selected R[k], and it is exchanged with the 1th record r[1] of the disordered region, so that the r[1..1 and R[2..N] respectively become the new ordered region with the number of records and the number of records reduced by 1.
......
③ I trip sort
At the beginning of the I-trip sort, the current ordered and disordered regions are r[1..i-1 and R[i respectively. N] (1≤i≤n-1). The trip sort selects the smallest key word record r[k] from the current unordered area, and exchanges it with the 1th record R[i] of the disordered region, so that the r[1..i] and R[I+1..N] become the new ordered region with the number of records added and the number of records reduced by 1.
In this way, the direct selection of n recorded files can be ordered by the n-1 of the direct selection order results.

Algorithm description

function Selectsort (arr) {//select sort-> Direct Selection
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;
}

Run Code Box

<style>
fieldset {
font-size:12px;
padding:10px;
width:80%;
Margin:auto;
}
Input {
font-size:12px;
Font-family:tahoma;
}
</style>
<title> Sort </title>

<fieldset>
<legend> Insert Sort </legend>

<p><b> Direct Insert Sort </b>
Please enter a sequence of characters to be sorted, separated by a half-width comma
<input name=insert type=text size=100 value= "G,v,u,f,p,o,i,a,t,j,e,l,k" >
<br><input Type=button value= "Sort" >

<p><b> Greek-children sort </b><br>
<input Name=shell type=text size=100 value= "G,v,u,f,p,o,i,a,t,j" >
<br><input Type=button value= "Sort" >

</fieldset>
<p>
<fieldset>
<legend> Exchange Sort </legend>

<b> Bubble Sort </b><br>
<input name=bubble type=text size=100 value= "G,v,u,f,p,o,i,a,t,j,e,l,k" >
<br><input Type=button value= "Sort" >

<p><b> Quick Sort <br>
</b>
<input name=quick type=text size=100 value= "3,1,5,4,6" >
<br><input Type=button value= "Sort" >

</fieldset>
<p>
<fieldset>
<legend> Select Sort </legend>

<b> Direct Selection Sort </b><br>
<input name=select1 type=text size=100 value= "G,v,u,f,p,o,i,a,t,j,e,l,k" >
<br><input Type=button value= "Sort" >

<p> .....

</fieldset>


<script>
function Insertsort (arr) {//Insertion sort-> Direct insertion method
var st = new Date ();
var temp, J;
for (var i=1; i<arr.length; i++) {
if ((Arr[i]) < (Arr[i-1]) {
temp = Arr[i];
j = i-1;
do {
ARR[J+1] = Arr[j];
j--;
}
while (j>-1 && (temp) < (ARR[J));
ARR[J+1] = temp;
}//endif
}
Status = (new Date ()-st) + ' MS ';
return arr;
}


function Shellsort (arr) {//insert sort-> Ashley 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) {//Ashley sort segmented execution function
  var temp, J;
  for (var i=d i<arr.length; i++) {
   if ((arr[i)) < (arr[i-d)) {
    temp = AR R[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 Bubblesort (arr) {//Interchange 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;
}

function Quicksortdemo (arr) {
var st = new Date ();
var result = QuickSort (arr);
Status = (new Date ()-st) + ' MS ';
return result;
}

function QuickSort (arr) {//Exchange sort-> Quick Sort
if (arguments.length>1) {
var low = arguments[1];
var high = arguments[2];
} else {
var low = 0;
var high = arr.length-1;
}
if (Low < high) {
function Partition
var i = low;
var j = high;
var pivot = Arr[i];
while (I&LT;J) {
while (I<j && arr[j]>=pivot)
j--;
if (I&LT;J)
arr[i++] = Arr[j];
while (I<j && arr[i]<=pivot)
i++;
if (I&LT;J)
arr[j--] = Arr[i];
}//endwhile
Arr[i] = pivot;
End Function
var pivotpos = i; Partition (Arr,low,high);
QuickSort (arr, Low, pivotpos-1);
QuickSort (arr, pivotpos+1, high);
} else
Return
return arr;
}

/*function Partition (arr, I, j) {//Quick sort, treats sorted array to divide
var pivot = Arr[i];
while (I&LT;J) {
while (Arr[j]>=pivot)
j--;
if (I&LT;J)
arr[i++] = Arr[j];
while (Arr[i]<=pivot)
i++;
if (I&LT;J)
arr[j--] = Arr[i];
}
Arr[i] = pivot;
return arr;
}*/

function Selectsort (arr) {//select sort-> Direct Selection
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;
}

function Unicode (str) {//Unicode code for string
var uni=0;
for (var i=0; i<str.length; i++) {
Uni + + str.charcodeat (i)/6553.5 * MATH.POW (str.length-i);
}
return uni;
}
</script>



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.