Heap sequencing of algorithms

Source: Internet
Author: User

Heap Sort

Heap: If each node of a complete binary tree is greater than (less than) its child nodes are called heaps.

K >= 2k + 1 && k >= 2k + 2

or k <= 2k + 1 && k <= 2k + 2

Heap points: Large top piles and small top piles.

Complete binary tree: All other nodes except the leaf node have a complete Saozi right sub-number, except for the last layer of non-leaf nodes.


Use heap sorting in two steps:

1 Build an unordered heap.

2 outputs the top element, then replaces the top element of the heap with the last element, and then becomes a heap.


For example, the process of using heap sorting for a number such as 3,22,8,11,55,1 is as follows:

1. Set up a fully binary tree in the order first:



Populates the data in hierarchical order, from the last non-leaf node to see if the heap requirements are met.

Start with 8, do not meet the heap requirements, and 1 transposition. (the node must be greater than or less than the left and right child nodes), we sort by the small top heap, so the nodes should be less than the left and right child nodes.


Again starting from 22, does not meet the requirements, and 11 transposition. (If the node is less than about, swap with smaller sub-nodes)


Again starting from 3, does not meet the requirements, and 1 interchange position.


So the heap is built. All nodes are not larger than their child nodes.


2 Second stage.

The top element of the heap is output from the initial heap, then the last element is placed on top of the heap, and then the definition of the heap is replaced.

The process of dropping the larger elements from the top to the bottom is called-------screening.


Output 1 First, then put 8 on the top of the heap


Then follow step 1 to filter it into a heap that matches the condition, knowing that 8 falls to the ground, that is, filter 8.

Replace with 3 first and 8 to ground. Compliant heap


Output 3, place 55 on the top of the heap


Then filter 55 to get the following


Output 8,,22 Heap Top


Filter 22, place 11 on top of heap,

Output 11, Output 22, output 55

The sort should be 1, 3, 8, 11, 22, 55


We can represent this complete binary tree with a one-dimensional array.

If the current node is k:

Then the parent node is: (k-1)/2

Left child: 2k + 1

Have children: 2k + 2


Its Java code is implemented as follows:

Package andy.test;/** * @author zhang,tianyou * @version November 7, 2014 PM 10:32:31 */public class Heapsorttest {public static void Main (string[] args) {int[] a = {3,22,8,11,55,1};heapsort (a);}  private static void Heapone (int[] A, int n, int k) {///node K filter//a: Heap data N: Number of valid data in heap K: filter node int k1 = 2*k + 1;//Left dial hand tree ordinal int K2 = 2*k + 2;//Right number ordinal if (K1 >= n && K2 >= N) return; Already a leaf node int a1 = Integer.max_value;int A2 = integer.max_value;if (K1 < n) a1 = A[k1]; Left child value if (K2 < n) a2 = A[k2]; Right child value if (A[k] <= A1 && A[k] <= A2) return; Already meet the requirements of the heap//find the smallest of the left and right children, and it swaps if (A1 < A2) {int temp = A[k];a[k] = A[k1];a[k1] = Temp;heapone (A, n, K1);//Continue filtering subtree}else{int te MP = A[k];a[k] = A[k2];a[k2] = Temp;heapone (A, n, K2);  Continue filtering subtree}}static void Heapsort (int[] a) {//Build initial heap//a.length last leaf node//a.length/2 last non-leaf node for (int i = A.length /2; I >= 0; i--) Heapone (A, a.length, i);//edge output heap top element, edge adjustment int n = a.length;//remaining element while (n > 0) {System.out.print (a[0] + "");//output heap top element a[0 ] = a[n-1]; //The last element is pinned N--;heapone (A, n, 0);//filter the first element}system.out.println ();}} 

The output is:

1 3 8 11 22 55

Heap sequencing of algorithms

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.