[Golang] Data structure-heap ordering

Source: Internet
Author: User

Next to Tree Select Sort
The previous article also said that the tree-type selection of sorting compared to simple selection of sorting, although reducing the complexity of time, but the use of more space to store the results of each round of comparisons, and each time will be compared with the winning node. The heap sort was invented by two big guys in 1964 to optimize the problem.

Principle
First there are a few definitions of the tree:

If the value of each node in a tree is greater than (less than) or equal to its byte point, then this tree is a large (small) root tree
If a large (small) root tree is exactly a binary tree, it is called Dagen (Keng Gen)

Heap sorting is done by using the Dagen (Keng Gen) feature.
From small to large sort general with Dagen, from large to small general with little Gan.

Process

    • The array to be sorted 8, 4, 12, 7, 35, 9, 22, 41, 2 is represented by a complete binary tree
    • According to the characteristics of the large root heap, this complete binary tree is compared to the last non-leaf node, and the larger value is exchanged to the current position. When the upper node order is encountered, the underlying nodes are not satisfied with the large root heap, then the underlying nodes are sorted. Eventually get the initial state of the big root heap.

    • The root node is then exchanged with the last leaf node

    • After swapping, the last leaf node is ignored, and the node of the tree is compared and exchanged to get the tree that meets the Dagen requirements again. Then continue to swap the root node with the last leaf node

    • And so on, you can finally get an ordered array

Complexity of
Average O (N*logn)
It is not appropriate to sort a small number of elements since the first time the Dagen is constructed with more order. Because nodes of the same numeric value cannot guarantee the order during the comparison process, it is an unstable sort method.

Code

Package Mainimport ("FMT" "Math/rand") func main () {var length = $ var tree []int for I: = 0; i < Leng Th i++ {tree = append (tree, int (rand). INTN ())} FMT. Println (tree)//The slice o at this time can be understood as the initial state binary tree number (Qie) group (Pian), and then need to adjust the order of the tree to the state of Dagen//Because it is from the lower right corner of the tree, the first non-leaf node from right to left from bottom to top comparison, So it is possible to know that the node from the n/2-1 position is counted for I: = LENGTH/2-1; I >= 0; i--{Nodesort (tree, I, length-1)}//times the tree is already a big heap. Simply swap the root node and the last node each time and reduce a comparison range. Another round of comparisons for i: = length-1; i > 0;        i--{//If only the root node and left child node are left, you can end up early if i = = 1 && tree[0] <= tree[i] {break} The value of the last node in the Exchange root node and the comparison range tree[0], tree[i] = Tree[i], tree[0]//Here recursively bring up the larger values layer by layer Nodesort (tree, 0, I -1) fmt. Println (tree)}}func nodesort (tree []int, startnode, Latestnode int) {var largerchild int leftchild: = Startnode   * * + 1 Rightchild: = leftchild + 1//Sub-node jumps out of the comparison range and recursively if Leftchild >= Latestnode {return} Left and right child nodes found in the larger, the child can not go beyond the range of comparison if Rightchild <= latestnode && tree[rightchild] > Tree[leftchild] { Largerchild = Rightchild} else {largerchild = leftchild}//At this point the Startnode node value is already the largest, no longer than the IF Tree[la  Rgerchild] <= Tree[startnode] {return}//To find here the child node value is larger than the parent node, so swap positions and continue to compare descendants until the big fish are fished up tree[startnode], Tree[largerchild] = Tree[largerchild], Tree[startnode] nodesort (tree, Largerchild, Latestnode)}

Note: Recursion in code is not an optimal solution.

Run results

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.