sorting | Sorting is called sorting, which is to organize the records in the file so that they are sorted by the keyword increment (or descending) order. The exact definition is as follows:
Input: N records r1,r2,...,rn, the corresponding key words are k1,k2,...,kn respectively.
Output: Ril,ri2,...,rin, make ki1≤ki2≤ ... ≤kin. (or ki1≥ki2≥ ...) ≥kin).
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 an integer d1 less than n as the first increment, dividing the entire record of the file into D1 groups. All records that are multiples of the DL are placed in the same group. First in each group of direct interpolation sort; Then, take the second increment d2<d1 repeat the grouping and sorting above until the incremental dt=1 (DT<DT-L<...<D2<D1) is taken, that is, all records are placed in the same group for direct insertion 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<J) {
while (I<j && arr[j]>=pivot)
j--;
if (I<J)
arr[i++] = Arr[j];
while (I<j && arr[i]<=pivot)
i++;
if (I<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;
}