C ++, C #, java Algorithm learning diary 06 ---- HeapSort)

Source: Internet
Author: User

C ++, C #, java Algorithm learning diary 06 ---- HeapSort)

What I learned in the previous articleHill sortingIt is an efficient optimization of insert sorting, while heap sorting is an efficient optimization of selection sorting. It is also a kind of selection sorting, so his basic idea is the sameSelect sort directlyThat is, the maximum or minimum value is selected from the sequence to the end or start, but the selection process of heap sorting isSelect sort directlyThe selection process is much more efficient. The tree structure is used to record the comparison results (repeated comparisons can be omitted), and the slave node is always the parent node (eventually forming a large root heap ), or a small value is always used as the parent node (eventually forming a small root heap), and the final heap top element is the maximum or minimum value.

Basic Idea:

Returns the array [0 .... n] changed to a large root heap (we sort the data in ascending order), that is, the array is adjusted to a complete binary tree with a parent node always greater than the child node, then, the array [0] (that is, the maximum value) on the top of the heap is exchanged with the array [n] on the end of the element (in this way, the just-captured Big root heap is messy ), adjust the array [0... array [n-1] is a large root heap. After n-1 loops, only one element array [0] is left.

From the process described above, we can see that heap sorting is mainly divided into two cycles,

1. Adjust the heap 2. Switch the beginning and end, and then adjust the heap:

In this article, we take the big root heap as an example (because we want to sort it in ascending order). We also mentioned that the big root heap is a complete binary tree with a parent node always greater than the child node, therefore, it has all the properties of a Complete Binary Tree. Therefore, if the parent node is array [I], its two child nodes are array [2 * I + 1] (left child ), array [2 * I + 2] (right child), and the parent node is greater than the left and right children.

For example, adjust array [] = {16, 7, 3, 20, 17, 8} to a large heap.

First, the array is represented:

Adjust from the last parent node array [2] (compare with array [5], exchange)

 

Final result:

(Large root heap) array [] = {20, 17, 8, 7, 16, 3}

 

We use the C ++ code to implement:

# Include
 
  
Using namespace std; void HeapAdjust (int * array, int nLength) {int I; int Child, temp; // I is the subscript of the parent node, from the last parent node to the first parent node for (I = (nLength/2-1); I> = 0; I --) {for (; 2 * I + 1
  
   

Result: 20 17 8 7 16 3

 

Adjust the first and end switches:

After adjusting the heap, we select the maximum value, that is, the array [0] on the top of the heap and the array [5]. Then, the disrupted heap is adjusted and exchanged, that is, the HeapAdujust () function is called for n-1 times.

 

 

This is the basic idea of heap sorting. Heap sorting is suitable for sorting massive amounts of data. Of course, you can define k forks to improve efficiency, but the idea is the same. In the next article, we will randomly generate 10000 numbers and use C ++, C #, and java to implement heap sorting and view records.

 

 

 

 

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.