Several sorting methods of javascript

Source: Internet
Author: User
Tags sort
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 directly inserting sorting, Xier sorting, bubble sorting, fast sorting, and directly selecting sorting. The code mentioned in this article is tested and passed in 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 <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;
}
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];
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.