Classic algorithm learning-heap sorting-PHP Tutorial

Source: Internet
Author: User
Classic algorithm learning-heap sorting. Classic algorithm learning-heap sorting is a sort that is a little more difficult than other sorting, and is a sort of selection by the nature of heap. Heap is actually a complete binary tree, as long as any classic algorithm is used for learning-heap sorting

Heap sorting is a kind of sorting that is less difficult than other sorting, and is a kind of selection sorting by the nature of heap. A heap is actually a complete binary tree. a heap can be formed as long as the keywords of any non-leaf node are not greater than or less than the left and right child nodes. A heap is divided into a large heap and a small heap. From the above properties, we can see that the keyword of the heap top of the big top heap is the largest among all keywords, and the keyword of the heap top of the small top heap is the smallest of all keywords. Heap sorting is not stable like fast sorting. Sample code uploaded to: https://github.com/chenyufeng1991/HeapSort

Heap sorting: The largest keyword (minimum keyword) is recorded on the top of a large heap (small top heap), so that the maximum record (minimum record) is selected from unordered each time) easy to use. Note: The Big Top heap constructs an incremental sequence, while the small top heap constructs a descending sequence.

(1) The initial sequence of keywords to be sorted (R0, R1.... Rn-1), to build a large top heap, this heap is the initial unordered area;

(2) swap the top element R [0] with the last element R [n-1] to obtain a new unordered zone (R0, R1 .... rn-2) and new ordered zone (Rn-1), and meet R [0, 1... n-2] <= R [n-1];

(3) because the new heap top R [0] after switching may violate the heap nature, the unordered zone (R0, R1... rn-2) is adjusted to the new heap, and then re-exchange the R [0] with the last element of the unordered zone to obtain the new unordered zone (R0, R1... rn-3) and new ordered zone (Rn-2, Rn-1 ). repeat this process to know that the number of elements in the ordered area is n-1, then the entire sorting process is completed.

The procedure is as follows:

(1) initialize the heap: [0... n-1] is constructed as a heap;

(2) swap the top element R [0] in the unordered zone with the last record in the interval, and then adjust the unordered zone to the new heap;

Therefore, the two most important operations for heap sorting are the construction of the initial heap and the adjustment of the heap. In fact, the construction of the initial heap is also the process of adjusting the heap, however, when the initial heap is constructed, all non-leaf nodes are adjusted.

The instance code is as follows:

/// Main. c // Train // Created by chenyufeng on 16/1/30. // Copyright©2016 chenyufengweb. All rights reserved. // # include
 
  
Void BuildHeap (int * a, int size); void swap (int * a, int * B); void HeapSort (int * a, int size ); void HeapAdjust (int * a, int I, int size); int main (int argc, const char * argv []) {int a [] = {3, 25, 9, 30, 2}; HeapSort (a, 5); for (int I = 0; I <5; I ++) {printf ("% d", a [I]);} return 0;} // Create a heap void BuildHeap (int * a, int size) {for (int I = size-1; I> = 0; I --) {HeapAdjust (a, I, size) ;}// exchange two void swap (int * a, int * B) {Int temp; temp = * a; * a = * B; * B = temp;} // heap sorting void HeapSort (int * a, int size) {BuildHeap (a, size); for (int I = size-1; I> = 0; I --) {// Swap heap top and last element, that is to say, each time the creator of the remaining element is placed behind it; swap (& a [0], & a [I + 1]); // re-adjust the heap to a large top heap; HeapAdjust (, 0, I) ;}// adjust Heid HeapAdjust (int * a, int I, int size) {int lchild = 2 * I; // left child node; int rchild = 2 * I + 1; // right child node; int max = I; if (I <= size) {if (lchild <= size & a [lchild]> A [max]) {max = lchild;} if (rchild <= size & a [rchild]> a [max]) {max = rchild;} if (I! = Max) {swap (& a [I], & a [max]); // avoid using the child tree with max as the parent node after adjustment as not a heap; HeapAdjust (a, max, size );}}}
 

Compared with other sorting methods, the sort heap sorting method is a kind of sorting method based on the nature of the heap. Heap is actually a complete binary tree, as long as any...

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.