Classical algorithm Learning--heap sorting

Source: Internet
Author: User

Heap sorting is a slightly troublesome sort relative to other sorts, and is a sort of selection that takes advantage of the nature of the heap. Heap is actually a completely binary tree, as long as any non-leaf node of the keyword is not greater than or not less than its left and right child nodes, you can form a heap. The heap is divided into large top piles and small top piles. By the above-mentioned properties, it is known that the key word of the heap top of the big Top heap is the largest of all the keywords, and the top of the heap of the small top heap is the smallest keyword in all the keywords. Heap sorting is the same as the fast sort, which is an unstable sort. Sample code upload to: Https://github.com/chenyufeng1991/HeapSort

The idea of heap sequencing: using a large top heap (small top heap) to record the maximum keyword (the minimum keyword) is a feature that makes it easy to select the maximum record (minimum record) each time from the unordered order. Note: The large top heap is constructed with an ascending sequence, and the small top heap is a descending sequence.

(1) The initial order to sort the keyword sequence (r0,r1 .... Rn-1), constructed into a large top heap, this heap is the initial disordered zone;

(2) Swap the top element of the heap r[0] with the last element r[n-1] and get a new unordered area (R0,r1 ...). Rn-2) and the new ordered area (Rn-1), and satisfies the r[0,1...n-2]<=r[n-1];

(3) due to the new heap top r[0] may violate the nature of the heap, it is necessary to the current unordered area (R0,R1 ... Rn-2) adjusts to the new heap, then swaps the r[0] with the last element of the unordered zone, resulting in a new unordered area (R0,R1 ... Rn-3) and the new ordered area (rn-2,rn-1). Repeat this process until you know that the number of elements in an ordered area is n-1, the entire sort process is complete.


The operation process is as follows:

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

(2) The top element of the current unordered area R[0] is exchanged with the last record of the interval, then the new unordered area is adjusted to the new heap;

So for heap sequencing, the most important two operations are to construct the initial heap and adjust the heap, in fact, the construction of the initial heap is also the process of adjusting the heap, but the initial heap is constructed to all non-leaf nodes are adjusted.

The instance code is as follows:

main.c//train////Created by Chenyufeng on 16/1/30.//copyright©2016 year chenyufengweb. All rights reserved.//#include <stdio.h>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;}    Build heap void buildheap (int *a,int size) {for (int i = size-1; I >= 0; i--) {Heapadjust (A, I, size);    }}//Exchange two number void swap (int *a,int *b) {int temp;    temp = *a;    *a = *b; *b = temp;}    Heap sort 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, each time the largest of the remaining elements is placed behind; Swap (&a[0], &a[i+1]);    Readjust the heap to a large top heap; Heapadjust (A, 0, i); }}//adjustment heap void heapadjust (int *a,int i,int size) {int lchild = 2 * i;//left child node; int rchild = 2 * i + 1;//right child node; int MA x =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 resizing the subtree with Max as the parent node is not a heap; Heapadjust (A, max, size); }    }}


This article references: http://blog.csdn.net/xiaoxiaoxuewen/article/details/7570621



Classical algorithm Learning--heap sorting

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.