# Sorting algorithm (ii) heap sequencing

Source: Internet
Author: User

First, select the problem of sorting

If there are N numbers ordered, a simple sort needs to pick an extremum (maximum or minimum) to compare n-1 times. However, each round of comparisons did not save the results of the previous comparison, resulting in the next round of comparisons will be compared to the data continue to compare the size, which actually affect the efficiency, do a lot of useless.

Heap sorting is an improvement to the simple selection sort.

A heap is a data structure that is constructed with a complete binary tree or a small top heap, some of which are called Dagen and small Gan.

Second, the preparation of heap sequencing knowledge

1, two fork tree

A binary tree is a tree structure in which there are at most two sub-trees.

Each node in the binary tree does not have more than 2 degrees (up to 2 subtrees trees)

2. Depth

The root node is the first layer, the child node of the root is the second layer, the second layer is the third layer, and so on.

The maximum level of nodes in a tree is called the depth or height of the tree.

3, full two fork tree

All branches of a binary tree have Saozi right subtrees, and all leaf nodes exist only at the bottom level.

In the same depth two-fork tree, the maximum of two-fork tree nodes.

K is depth (1≤k≤n), the total number of nodes is 2^k-1.

For example, a full two fork tree with 15 nodes at a depth of 4:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/76/wKioL1cfmQKyxnbfAAByxclOvew262.png "title=" 001. PNG "alt=" Wkiol1cfmqkyxnbfaabyxclovew262.png "/>

4. Fully binary tree (complete binary trees)

If the depth of the binary tree is k, the number of nodes of the binary tree from 1 to the K-1 layer reaches the maximum number, and all nodes in the K layer are concentrated on the leftmost side, which is the complete binary tree.

A complete binary tree is drawn from a tree full of two forks.

A full two fork tree must be a completely binary tree, but a complete binary tree is not full of two forks.

K is the depth (1≤k≤n), the maximum number of nodes is 2^k-1, when the maximum value is full of two fork tree.

For example, the following diagram is a complete binary tree, the bottom layer of the leaf nodes are continuously concentrated on the left.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/79/wKiom1cfmFvzXzYBAABhaX2vmB4140.png "style=" float: none; "title=" 002.png "alt=" Wkiom1cfmfvzxzybaabhax2vmb4140.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/79/wKiom1cfmFvASY1FAABkLBygeQQ346.png "style=" float: none; "title=" 003.png "alt=" Wkiom1cfmfvasy1faabklbygeqq346.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/76/wKioL1cfmSbRizzJAABorioSjnc314.png "style=" float: none; "title=" 004.png "alt=" Wkiol1cfmsbrizzjaaboriosjnc314.png "/>

The following figure is not a complete binary tree, because its nodes are discontinuous at the bottom of the layer.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/79/wKiom1cfmICRW48pAABu1G-S3lU381.png "style=" float: none; "title=" 005.png "alt=" Wkiom1cfmicrw48paabu1g-s3lu381.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/76/wKioL1cfmUuBHlZ_AABvxwUWyIg714.png "style=" float: none; "title=" 006.png "alt=" Wkiol1cfmuubhlz_aabvxwuwyig714.png "/>

5. Large top piles and small top piles

A data structure built using a complete binary tree.

(1) Large top pile

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7F/76/wKioL1cfmV6A7wDcAABrqLMBbds236.png "title=" 007. PNG "alt=" Wkiol1cfmv6a7wdcaabrqlmbbds236.png "/>

The first must be a fully binary tree, requiring that the value of each non-leaf node be greater than or equal to the value of all its child nodes.

(2) Small top pile

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/79/wKiom1cfmKnDcygTAABr0_v4fpo339.png "title=" 008. PNG "alt=" Wkiom1cfmkndcygtaabr0_v4fpo339.png "/>

The small top heap must also be a complete binary tree, requiring that the value of each non-leaf node be less than or equal to the value of all its child nodes.

Third, heap sort (heap sort)

With the basic knowledge above, you can start the heap sort, this test uses the Big top heap sort

(a) algorithm ideas

1. Build a binary tree

Place the numbers you want to sort in a full binary tree. Because of the use of a fully binary tree, it is convenient to use sequential storage to describe the complete binary tree, which is to use an array to represent a complete binary tree.

Assuming that the number to be sorted is {30,20,80,40,50,10,60,70,90}, the binary tree is represented as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/76/wKioL1cfmZmC8B3qAABud25gcBY121.png "title=" 010. PNG "alt=" Wkiol1cfmzmc8b3qaabud25gcby121.png "/>

For ease of calculation, use the following array to represent {0,30,20,80,40,50,10,60,70,90}. The ordinal of the array is exactly the subscript of the arrays.

2. Building a large top pile

(1) Core algorithm

Select the starting node A to compare the node to its child nodes.

Suppose a has two sub-nodes B and C, if the maximum value in the child node is greater than the value of Node A, then the maximum node and node a Exchange position.

Suppose a has only one child node B, and if the value of B is greater than the value of Node A, the position is swapped. The case of such a sub-node is the simplification of the 2 sub-nodes above.

If Node A still has sub-nodes C and D at the new location after the interchange, repeat the above procedure to compare nodes A to node C and Node D.

(2) Choice of starting point

Build a big Top heap, from which node to compare?

In simple terms, start with the parent node of the rightmost leaf node of the bottom layer of the complete binary tree.

Begins at the third-level left node.

For convenience of calculation, the array is specially constructed, thus having the following relationship.

Parent-child node relationship: The subscript of the parents node is I, and the sub-nodes are labeled 2i and 2i+1.

The relationship between the starting node and the total N: N corresponds to the subscript of the last leaf node. Because the sub-node 2i or (2i+1) calculates the parent node subscript, 2I/2 or (2i+1)/2 equals I, so N/2 is the subscript for the starting point.

(3) Selection of the next node

After comparison, look for the next node to continue the comparison. From the current comparison node to the left to find Brothers node, if this layer is finished, find a layer of the rightmost node, until the root node is found.

After the construction of the large top heap is completed, the tree structure

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/76/wKioL1cfoAyBE1GmAABsCMINkhA079.png "title=" 011. PNG "alt=" Wkiol1cfoaybe1gmaabscminkha079.png "/>

3. Sorting

This is an iterative process. Since the use of the large top heap, each exchange has a lot of nodes do not need to move, this fully embodies the idea of preserving the results of the previous sort.

(1) Swap the root node and the last leaf node of the Big Top heap, then the last leaf node is the maximum value, excluding the leaf nodes from the sorted nodes (because this is the maximum value that is found each time).

(2) from the root node (the new root node), re-adjust to the big top heap, repeat the previous step.

Each round of the new root node is adjusted to the large top heap of the process and the above construction of the big top heap of the core algorithm is the same, because the same function, but the beginning of the selection of different, the construction of the big top heap process from the N/2 began to reverse the adjustment, and heap sorting always start from the root node.

(ii) Code implementation (Java)

`package heapsort;import java.util.arrays;import java.util.hashset;public class  heapsort {    //  in order to calculate the easy to remove the label, add a 0 in the first     //  to compare the number of n is 9     //public static int[] origin = new int[]{ 0,30,20,80,40,50,10,60,70,90};    public static int[] origin =  The New int[]{0,50,10,90,30,70,40,80,60,20};    // indexset array records the index of each changed node, just to make it easier to observe     private static HashSet<Integer> indexSet = new  Hashset<integer> ();     public static void main (String[] args)  {        int total = origin.length-1;         system.out.println ("unsorted: " +arrays.tostring (Origin));         //&nbSP; build Big Top pile         for  (int i = total/2,k=1; i  >= 1; i--, k++)  {             heapadjust (total,i,origin);             //   Print the results of each adjustment and the subscript of the Exchange              System.out.println (k +  "\ T"  + arrays.tostring (Origin)  +  " "  +  Arrays.tostring (Indexset.toarray ()));        }         system.out.println ();        //  heap sort          for  (;total>1;)  {             system.out.println (total);             //  Exchange root node and lastA leaf knot point             swap (1, total, origin) ;             system.out.println ("Swapped: "  + arrays.tostring (origin));               Total minus 1, because the last leaf node is now the maximum value to exclude from sorting numbers              total--;            if  (total==2  &AMP;&AMP;&NBSP;ORIGIN[TOTAL]&GT;=ORIGIN[TOTAL-1])  {                 //  Improvement: If only 2 nodes are compared, and the second node is greater than or equal to the root node, there is no need to adjust again, you can exit directly                  break;             }             heApadjust (Total,1,origin);            //  Print the results of each adjustment and the subscript             system.out.println of the interchange (" changed:  " + arrays.tostring (Origin)  + "   " + arrays.tostring ( Indexset.toarray ()));             System.out.println ();        }         system.out.println ("sorted: " +arrays.tostring (Origin));    }     /**     * adjustHeap  adjustment for large top pile structure      *   @param  n  number to compare      *  @param  index  node subscript for each comparison       *  @param  array  Arrays to compare      */     public static void heapadjust (int n,int index,int [] array) {        indexset.clear ( );        int maxchildrenindex;         for  (Int i = 2*index; i <= n; i = index &LT;&LT;1)  {            maxchildrenindex  = i;            if  (i<n  &AMP;&AMP;&NBSP;ARRAY[I]&LT;ARRAY[I+1])  {                 //  less than n indicates that there is a node on the right, which selects the subscript of the maximal sub-node                  maxChildrenIndex=i+1;             }             //  equals n, indicating that there is only one child node, less than N,At the same time, the left node is greater than the right node, neither of these situations maxindex change.             if  (array[maxchildrenindex]> Array[index])  {                 swap (Index, maxchildrenindex, array);                 //  Record Exchange node index for easy observation                  indexset.add (Index);                 indexset.add (Maxchildrenindex);                 //  after adjustment, you need to reset the starting node of the comparison, See if this node and its sub-nodes need to be adjusted                  index = maxchildrenindex;            }            else {                 break;             }        }     }    public static void swap (int index1,int  Index2,int [] array) {        try {             int temp = origin[index1];             origin[index1] = origin[index2];             origin[index2] = temp;         } catch  (exception e)  {          &nbsP;  throw new arrayindexoutofboundsexception ();         }    }}`

(iii) Summary

1. Complexity of Time

Assuming that the node is n, the depth of the full binary tree is K

In the process of building the heap, because we are completely binary tree from the bottom right of the non-terminal node to build, compare it with their children and if necessary interchange, for each non-terminal node, in fact, two times the comparison and interchange operations, so the entire build heap time complexity is O (n).

In the formal order, the first time to fetch the heap top record to rebuild the heap requires O (Logn), and the need to cycle through the n-1 heap top record, therefore, the time complexity of rebuilding the heap is O (Nlogn).

So in general, the time complexity of heap sequencing is O (NLOGN). Because heap sorting is not sensitive to the sequencing state of the original record, it is O (Nlogn), both the best, worst, and average time complexity.

2. Complexity of space

It uses a swap space with an O (1) space complexity.

3. Stability

An unstable sorting method.

Reference

Http://baike.baidu.com/view/157305.htm

"Big Talk Data Structure"

Sorting algorithm (ii) heap sequencing

Related Keywords:

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.