JavaScript data structure and algorithm--Advanced sorting algorithm

Source: Internet
Author: User

JavaScript data structure and algorithm--Advanced sorting algorithm

The advanced sorting algorithm is the most efficient sorting algorithm for working with large datasets, and it is the processing of datasets that can reach millions of elements, not just hundreds of or thousands of. Now let's learn the next 2 advanced sorting algorithms----Hill sort and quick sort.

One: Hill sort;

The core idea of the hill sort is to compare elements that are farther apart than adjacent elements first.

Rationale: By defining an interval sequence to represent how far apart the elements are compared during the sorting process.

Let's take a look at the array [0,9,1,8,7,6,2,3,5,4] to use the principle of hill sorting, such as:

The code analysis is as follows:

1. Perform "Steps for interval sequence =3"

A. So the first time to perform the "Interval sequence = 3" step, then find the location Interval 3 element is 0, 8, 2, 4

so the four number of 0 is the smallest, so the position is not moved, or the first position, 8 is the largest so the position is moved to the previous 4 position, 4 moved to 8 position, but 2 and 4 compare 2 is less than 4, so 2 and 4 positions continue to swap. So now the order is 0, 2, 4, 8, such as (interval sequence =3)

B. Now the second time to perform the "Interval sequence = 3" step, find the element is 9, 7, 3 analysis or the same as the above 3 is the smallest, so move to the original 9 position, 9 moved to the position of 3, because 7 is less than 9 so do not need to move, so now the order is 3, 7, 9 such as (interval sequence =3)

C. Now the third time to perform the "Interval sequence = 3" step, find the element 1, 6, 5, because 1 is the smallest so do not need to move, 6 is greater than 5, so 2 to swap positions so the order is 1, 5, 6

After performing the above steps, the correct order is [0,3,1,2,7,5,4,9,6,8]

2. To perform the "Interval sequence = 2" step

A. The first find element 0,1,7,4,6, so the principle is the same as above, 0 and 1 do not need to move 7 and 4 and 6 swap positions, so now the order becomes 0,1,4,6,7

B. Second lookup finds element 3,2,5,9,8, so order becomes 2,3,5,8,9

After performing the above steps, the correct order is [0,2,1,3,4,5,6,8,7,9] such as (interval sequence =2)

3. Perform the steps of "interval sequence = 1";

The order of the arrays becomes [0,1,2,3,4,5,6,7,8,9]. such as (interval sequence =1)

The JS code is as follows:

functionCArray (numelements,gaps) { This. DataStore = [];  This. pos = 0;  This. numelements =numelements;  This. Gaps =gaps;  This. Insert =Insert;  This. toString =toString;  This. Clear =Clear;  This. Shellsort =Shellsort;  for(vari = 0; i < numelements.length; i++) {         This. datastore[i] =Numelements[i]; }            }functionClear () { for(vari = 0; I < This. datastore.length; i++) {         This. datastore[i] = 0; }}functionInsert (Element) { This. datastore[ This. pos++] =element;}functiontoString () {varRestr = "";  for(vari = 0; I < This. datastore.length; i++) {Restr+= This. Datastore[i] + ""; if(i > 0 && i% 10 = = 0) {Restr+ = "\ n"; }    }    returnRestr;} functionShellsort () { for(varg = 0; G < This. gaps.length; ++g) { for(vari = This. Gaps[g]; I < This. datastore.length; ++i) {vartemp = This. Datastore[i];  for(varj = i; J >= This. gaps[g] && This. DATASTORE[J- This. Gaps[g]] > temp; J-= This. Gaps[g]) {                 This. datastore[j] = This. DATASTORE[J- This. Gaps[g]]; }             This. datastore[j] =temp; }    }}//Hill Sort test Codevarnumelements = [0,9,1,8,7,6,2,3,5,4];vargaps = [3,2,1];varMynums =NewCArray (numelements,gaps); Mynums.shellsort (); Console.log (mynums.tostring ());

Two: Quick sort

Fast sorting is one of the fastest sorting algorithms for working with large data sets. It is a divide-and-conquer algorithm that recursively decomposes data into distinct sub-sequences that contain smaller elements and larger elements. The algorithm repeats this step until all the data is in order.

The algorithm first selects an element in the list as the base value, the data is sorted around the base value, the element that is less than the base value in the list is moved to the left of the array, and the element that is larger than the base value is moved to the right of the array, as shown in:

The rationale is:

    1. Select a datum element to split the list into 2 sub-sequences. There are 3 ways to select a datum element, you can select the first element as a datum element, you can use the middle value as a datum element, and you can use the last element as the base element.
    2. Reorder the list, placing all elements that are less than the base value in front of the datum, and all elements that are larger than the base value after the Datum.
    3. Repeat steps 1 and 2, respectively, for the sub-sequences of smaller elements and for the subsequence of larger elements.

Here is the JS code as follows:

//Quick SortfunctionQSort (list) {if(List.length = = 0) {        return []; }    //store values that are less than the base value    varleft = []; //store values greater than the base value    varright = []; varPivot = list[0];  for(vari = 1; i < list.length; i++) {        if(List[i] <pivot)        {Left.push (list[i]); }Else{Right.push (List[i])}}returnQSort (left). Concat (Pivot,qsort);}//Quick Sort Test Codevarnumelements = [44,75,23,43,55,12,64,77,33];varList =QSort (numelements); Console.log (list); //[ up to, at, +,----]

JavaScript data structures and algorithms-advanced sorting algorithms

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.