Heap sequencing (Golang implementation)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Encapsulated into functions:

//堆排序//s[0]不用,实际元素从角标1开始//父节点元素大于子节点元素//左子节点角标为2*k//右子节点角标为2*k+1//父节点角标为k/2func HeapSort(s []int) {    N := len(s) - 1 //s[0]不用,实际元素数量和最后一个元素的角标都为N    //构造堆    //如果给两个已构造好的堆添加一个共同父节点,    //将新添加的节点作一次下沉将构造一个新堆,    //由于叶子节点都可看作一个构造好的堆,所以    //可以从最后一个非叶子节点开始下沉,直至    //根节点,最后一个非叶子节点是最后一个叶子    //节点的父节点,角标为N/2    for k := N / 2; k >= 1; k-- {        sink(s, k, N)    }    //下沉排序    for N > 1 {        swap(s, 1, N) //将大的放在数组后面,升序排序        N--        sink(s, 1, N)    }} //下沉(由上至下的堆有序化)func sink(s []int, k, N int) {    for {        i := 2 * k        if i > N { //保证该节点是非叶子节点            break        }        if i < N && s[i+1] > s[i] { //选择较大的子节点            i++        }        if s[k] >= s[i] { //没下沉到底就构造好堆了            break        }        swap(s, k, i)        k = i    }} func swap(s []int, i int, j int) {    s[i], s[j] = s[j], s[i]}

Test:

s := []int{-1,9, 0, 6, 5, 8, 2, 1, 7, 4, 3}fmt.Println(s[1:])HeapSort(s)fmt.Println(s[1:])

Output:
[9 0 6 5 8 2 1 7 4 3]
[0 1 2 3 4 5 6 7 8 9]

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.