The implementation of heap sort in go language

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

For the algorithm of heap sorting, you can refer to my last article, "Heap sort". The article is about setting up a small top heap to carry out the sort, which is said to build a large top heap to build the sort, almost.

In the Golang source sort package, the sorting function comes with it. The function can sort the various types, except that the type needs to implement three functions that enable the class to implement the Interface interface.

type Interface interface {// Len is the number of elements in the collection.Len() int// Less reports whether the element with// index i should sort before the element with index j.Less(i, j int) bool// Swap swaps the elements with indexes i and j.Swap(i, j int)}

These three functions are, respectively, get the sort queue Length, the queue any two element compare size, and exchange any two elements.

func (a BySortIndex) Len() int      { return len(a) }func (a BySortIndex) Swap(i, j int) { a[i], a[j] = a[j], a[i] }func (a BySortIndex) Less(i, j int) bool {return a[i] < a[j]}

If it is an array of shapes, it can be implemented as above. Golang supports multi-value assignment, so the interchange value is simple. lenIt also makes the length traversal very simple. Compare size, can be defined according to the actual situation.

The core of heap sorting is to build a large top heap and exchange values, which are local sort and do not require new allocation space. Golang source code I have added a note, it is not difficult, you can read directly.

func heapSort(data Interface, a, b int) {first := alo := 0hi := b - a// Build heap with greatest element at top.for i := (hi - 1) / 2; i >= 0; i-- {siftDown(data, i, hi, first)}// Pop elements, largest first, into end of data.// 二叉树结构当中最后一个有子结点的结点for i := hi - 1; i >= 0; i-- {data.Swap(first, first+i)siftDown(data, lo, i, first)}}// 建立树函数// 父节点root的左孩子2*root + 1func siftDown(data Interface, lo, hi, first int) {root := lofor {child := 2*root + 1if child >= hi { // child 超出了数组长度,也就是该结点无孩子结点,返回break}if child+1 < hi && data.Less(first+child, first+child+1) { // 右孩子结点存在child++}// 以上是为了在孩子结点当中找到较大的结点,用child表示if !data.Less(first+root, first+child) {return}// 如果根结点小于较大的孩子结点,则进行交换;该孩子结点的堆结构可能会被影响,继续去处理孩子结点data.Swap(first+root, first+child)root = child}}

Please refer to the complete source code in this article.

Reference documents

    • "1" heap sort (heap sort)-Cyeam

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.