Data structure _ Heap sorting introduction

Source: Internet
Author: User

The original http://www.cnblogs.com/skywang12345/p/3602162.html. On the basis of which he modified a little Heap Ordering Introduction

Heap ordering (heap sort) refers to a sort algorithm designed using the data structure of the heap.
So, before learning heap sequencing, it is necessary to understand the heap! If the reader is not familiar with the heap, it is recommended to understand the heap first (it is recommended that you understand it through a binary heap, a left-leaning heap, an oblique heap, a two-item heap, or a Fibonacci heap), and then learn this chapter.

We know that the heap is divided into "maximum heap" and "minimum heap". The maximum heap is usually used for "ascending" sorting, while the smallest heap is usually used for "descending" sorting.
Given that the maximum heap and minimum heap are symmetric relationships, one can be understood. This article provides a detailed description of the ascending sort for the maximum heap implementation.

The basic idea of the maximum heap for ascending sorting:
① initializes the heap: constructs a sequence of A[1...N] into the largest heap.
② Exchange data: Swap a[1] and a[n], so that a[n] is the maximum value in A[1...N, and then resize A[1...n-1] back to the maximum heap. Next, a[1] and a[n-1] are exchanged, so that a[n-1] is the maximum value in a[1...n-1], and then the a[1...n-2] is resized to the maximum value. And so on, until the whole sequence is orderly.

Below, the implementation of heap sequencing is parsed by graphic. Note that the implementation uses the "property of the two-fork heap implemented by the array".
In the case where the first element has an index of 0:
Nature One: Index of the left child of the index is (2*i+1);
Nature Two: Index of the left child of the index is (2*i+2);
Property Three: The index of the parent node indexed to I is floor ((i-1)/2);

Heap Sort Personal Summary:

The first step to build the heap downward adjustment algorithm A for loop starts from N/2 minus;
The second step is to adjust the sequence starting from the last element, narrowing the adjustment until the first element
The function of Heap_sort_asc (A, N) is to sort the array a in ascending order, where a is the arrays and n is the length of the array. The operation is divided into two parts: initializing the heap and exchanging data. = two for loop
Maxheap_down (A, start, end) is the downward adjustment algorithm for the maximum heap . = A For loop, several If-else

#include <stdio.h>//Array Length#defineLENGTH (Array) (sizeof (array))/(sizeof (ARRAY[0)))#defineSwap (A, b) (a^=b,b^=a,a^=b)/** (max) heap down adjustment algorithm * * Note: Array implemented in the heap, nth node of the left child's index value is (2n+1), the right child's index is (2n+2). * where n is an array subscript index value, and the 1th number in the array corresponds to n 0. * * Parameter Description: * A--array to be sorted * Start--The starting position of the node being lowered (typically 0, indicating starting from 1th) * End-up to range (typically the index of the last element in the array)*/voidMaxheap_down (intA[],intStartintend) {    intStart1 = start;//start position of current node    intPosition =2*start1 +1;//left child's position    intTMP = A[start1];//the current node size     for(;p osition<=end;) {        if(position<end&&a[position]<a[position+1]) position++; if(a[position]<=tmp) Break; Else//Exchange{A[start1]=a[position];//move a larger value upa[position]=tmp;//The TMP is also moved down} start1=position;//at the same time Start1 becomes a newposition=2*position+1;//Left child also changes    }    }/** Heap sort (small to large) * * Parameter Description: * A--the array to be sorted * N--The length of the array*/voidHEAP_SORT_ASC (intA[],intN) {    inti; //The first step to build a downward adjustment algorithm for the heap     for(i=n/2; i>=0; i--) {Maxheap_down (A,i,n-1);//The first step is to carry out all the    }    //The second step is to adjust the sequence starting from the last element, narrowing the adjustment until the first element     for(i=n-1;i>0; i--) {Swap (a[0],a[i]);//swap once to place the maximum value on the last surface of the array a[i];Maxheap_down (A,0, I-1);//remove one of the most subsequent array elements to rebuild the heap    }}voidMain () {inti; intA[] = { -, -, -, +, -, the, -,Ten, -, -, the}; intIlen =LENGTH (a); printf ("before sort:");  for(i=0; i<ilen; i++) printf ("%d", A[i]); printf ("\ n");            Heap_sort_asc (A, Ilen); //Ascending order//Heap_sort_desc (A, Ilen); //Descending arrangementprintf ("After sort:");  for(i=0; i<ilen; i++) printf ("%d", A[i]); printf ("\ n");}

Data structure _ Heap sorting introduction

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.