[Golang] Data structure-Hill sort

Source: Internet
Author: User

In addition to the binary insertion sort described in the previous article, there is also the hill sort (Shell's sort) introduced this time, which is also an optimization of the direct insertion sorting algorithm.

Principle
Hill sort, that is, the data is grouped by an increment value, each group is sorted separately, then the increment is reduced, and then each group is sorted by the new amount after grouping the data. When the final increase is reduced to 1, the sort ends. So the hill sort is also called narrowing the incremental sort (diminishing Increment sort)

About increments
The choice of the best increment value is actually a math problem, interested can search the relevant information on their own.
The common increment has N/2 (this is called Hill Increment), N/3, 2^k-1 (Hibbard increment) and so on, the actual use of slightly revised increment may also make the performance of the sorting very large fluctuations.
For example, using N/2 increment, that is, the initial increment is LENGTH/2, the second round is divided 2:LENGTH/4, until the increment value becomes 1

Process
Suppose there are arrays: [8,12,6,33,12,5,4,94,63,23,75,22,43,27,46], in N/2 increments, then the whole sort process is this:

Complexity of
Different incremental complexity. The average time complexity for N/2 is O (n^2).
Compared with the direct insertion of the sort, hill sort reduced the number of comparisons and exchanges, and in small and medium-sized sorting, performance was better. But as the amount of data increases, there is still a big gap between hill sorting and other better sorting algorithms (fast, heap, and normalized).

Code

package mainimport (    "fmt"    "time"    "math/rand")func main() {    var length = 15    var list []int    // 以时间戳为种子生成随机数,保证每次运行数据不重复    r := rand.New(rand.NewSource(time.Now().UnixNano()))    for i := 0; i < length; i++ {        list = append(list, int(r.Intn(1000)))    }    fmt.Println(list)    // 这里就以n/2为增量z    gap := 2    step := length / gap    for step >= 1 {        // 这里按步长开始每个分组的排序        for i := step; i < length; i++ {            // 将按步长分组的子队列用直接插入排序算法进行排序            insertionSortByStep(list, step)        }        // 完成一轮后再次缩小增量        step /= gap        // 输出每轮缩小增量各组排序后的结果        fmt.Println(list)    }}// 这里把上篇直接选择排序的算法抽出来,并将步长从1改成stepfunc insertionSortByStep(tree []int, step int) {    for i := step; i < len(tree); i++ {        for j := i; j >= step && tree[j] < tree[j-step]; j -= step {            tree[j], tree[j-step] = tree[j-step], tree[j]        }    }}

Run results

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.