"JavaScript algorithm"---hill sort

Source: Internet
Author: User

One, what is the sort of hill

The Hill sort (Shell's sort) is a more efficient and improved version of the Insert Sort algorithm, also known as "narrowing incremental sorting".   Ideas:     Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increment decreases gradually, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates   Logic:    The most important thing in the hill sort is the grouping, we first find an interval, each interval to place these numbers in a set of,    Suppose there is such an array  [9,1,2,5,7,4,8,6,3,5]  ; every 5 of us is a group. So this array is divided into the following groups     first group:9    4    second group:1   8    third group:2   6& nbsp   Fourth group:5   3    Fifth group:7   5        After the group is complete we sort in groups of every group  & nbsp;     First group: 4   9    second group:1    8    Third group     2  &nbs P 6    Fourth group     3    5    Fifth group     5    7     Sort after we're going to This array resets the interval to reorder (halve the next interval)   2 for a group     [4,1,2,3,5,9,8,6,5,7]          The first group: 4   2  5  8   5   == Group sort    2 4 5 5 8    &NBSp; second group:1  3  9  6   7   == Group sort    1 3 6 7 9       &NB Sp 2  1  4  3  5  6  5  7  8  9          sort after I We re-order this array again (half the next interval)   1 for a group the last time that is 22 comparison         1  2  3  4  5& nbsp  5  6  7  8  9      Note: Intervals are generally not specified in the hill sort, typically half the length of the array    two, code
var arr = [2,5,1,9,0]// calculate interval var len = Math.floor (ARR.LENGTH/2) while (len>0) {    for (var i=len;i<arr.length;i++) {        var temp = arr[i];          for (var j=i-len;j>=0&&temp<arr[j];j=j-len) {            arr[j+len] = arr[j]        }        arr[j+len] = temp;    }     = Math.floor (LEN/2)}

JavaScript algorithm---Hill sort

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.