Heap sort Python implementation (RPM) __python

Source: Internet
Author: User
Heap Sort

Heap ordering is a sort of tree selection, which is an effective improvement of direct selection sort.
Basic idea:
The heap is defined as follows: A sequence with n elements (k1,k2,..., kn), if and only if satisfied

Case 1:ki <= k2i and Ki <= k2i+1 (minimized heap or small top heap: The value of the left and right child is greater than that of the parent node)
Case 2:ki >= k2i and Ki >= k2i+1 (maximize heap or top heap: The value of the left and right child is smaller than the parent node)
wherein the i=1,2,..., N/2 downward to be rounded;

(1)] is called a heap. As you can see from the definition of the heap, the top element of the heap (that is, the first element) must be the smallest item (the small top heap). If a heap is stored in a one-dimensional array, the heap corresponds to a complete binary tree, and the value of all non-leaf nodes is not greater than (or not less than) the value of their children, and the value of the root node (the top element of the heap) is the smallest (or largest). Such as:
(a) Large top heap sequence: (96, 83,27,38,11,09)
(b) Small top heap sequence: (12,36,24,85,47,30,53,91)

Initially, the sequence of n numbers to be sorted is considered to be a sequential two-tree (one-dimensional array storage binary tree), adjust their storage order to become a heap, output the top element of the heap, and get the smallest (or largest) element in the N element, the minimum (or maximum) number of the root node of the heap. Then the front (n-1) element is readjusted to make it a heap, outputting the top element of the heap, and getting the second small (or second) element of n elements. And so on, until there are only two nodes in the heap, and exchange them, and finally get an ordered sequence of n nodes. Call this process a heap sort .
Therefore, the implementation of heap sorting needs to solve two problems: 1. How to build a heap of n-sorted numbers; 2. After outputting the heap top element, how to adjust the remaining n-1 elements to make it a new heap.
First, we'll discuss the second problem: after the top element is output, the remaining n-1 elements are restructured in a stack of adjustment processes. How to adjust the small top heap:
1 with m elements of the heap, output heap top elements, left m-1 elements. The heap bottom element is fed to the top of the heap (the last element is exchanged with the top of the heap) and the heap is corrupted because the root node does not satisfy the nature of the heap.
2 The root node is exchanged with the smaller elements in the left and right subtree.
3 If the Zuozi Exchange: If the Shozi is destroyed, that is, Zuozi root node does not meet the nature of the heap, then repeat the method (2).
4 If it is exchanged with the right subtree, if the right subtree is destroyed, the root node of the right subtree does not satisfy the nature of the heap. The Repeat method (2).
5 continue to carry on the above exchange operation to the subtree that does not satisfy the heap nature, until the leaf node, the heap is built.

The adjustment process of the root node to the leaf node is screened.
The process of building the initial heap on n elements is discussed again. Building a Heap method: the process of building a heap on an initial sequence is a process of filtering over and over again.
1 A complete binary tree of n nodes, the last node is a subtree of the [N/2] node.
2 filter begins with the subtree of the [N/2] node as the root, and the subtree becomes the heap.
3) Then the tree of the root of each node is filtered to make it a heap until the root node.

"" Heap sort does not implement "" "Def Showarr (a): n = Len (a); For I in range (n): Print (a[i],end= "") print ("\ n") def Build_heap (a): i = (Len (a)-1)//2 while i>=
    0:heap_adjust (A,i,len (a)) I = i-1 return a def heap_adjust (h,s,n): print (s) temp = H[s] Child = 2*s+1 # #左孩子节点 root node starting from 0, the right children node is child+1 while baby < N:if Child+1<n and H[child] 
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.