Sort algorithm--heap sort

Source: Internet
Author: User

Heap ordering is the ordering of a given sequence as a sequential storage structure of a complete binary tree .

Before learning heap sequencing, let's look at one of the properties of a fully binary tree:

Given a completely binary tree BT, using sequential storage structure for storage, then how to represent the relationship between the parent node and the left and right child nodes?

Here are two things:

(a). If the storage is started from the position of the subscript 0, then for the node labeled I, the left child's subscript is 2i+1, the right child's subscript is 2i+2, the parent node subscript is (i-1)/2

(b). If the storage is started from the position of subscript 1, then for the node labeled I, the left child's subscript is 2i, the right child's subscript is 2i+1, and the parent node subscript is I/2.

To meet our habit of starting storage from subscript 0, here's the first way. Thus, the heap can be defined: given a sequence K0, K1 、...、 Kn-1, if Ki≤k2i+1 and ki≤k2i+2 are met ,

is called the small Gan, if satisfies ki≥k2i+1 and ki≥k2i+2, is called Dagen .

As described above for the basic concept of the heap and related knowledge, now we discuss the sort of Dagen .

For a large heap, the root node is the largest element, selecting the largest element and swapping it with the last element of the sequence so that the largest element succeeds in homing. But at this point the structure of the heap

has been compromised, the element's position needs to be re-adjusted to satisfy the nature of the heap again . Therefore, for heap ordering, the heap adjustment algorithm is its core and essence .

Heap Tuning algorithm:

#include <stdio.h>#include<stdlib.h>voidAdjustment (intA[],intSintt) {    intI, J, temp; I=s; J=2*i+1;//J is the subscript for the left childtemp = A[i];//Helper Variable Temp Saves the value of the parent node     while(J <=t) {if(J < T && A[j] < a[j+1]) J++;//If the right child is larger than the left child, let J save the right child's subscript        if(Temp < A[J])//if the value of the parent node is less than the value of the child's node{A[i]= A[j];//will let the child node swap to the parent node's locationi =J; J=2*i +1; }        Else             Break;//satisfying the nature of Dagen, jumping out of the loop} A[i]= temp;//inserts the value of the saved parent node into the appropriate location}

When the initial heap is established, it needs to be adjusted from the last non-leaf node to the back-forward until the entire sequence satisfies the nature of the heap. At this point, the root node is the largest element, and the root node is the last leaf node.

Swapping locations, leaving the largest elements in place, but destroying the structure of the heap, requires that the rest of the N-1 elements call the heap's tuning algorithm to adjust, so repeatedly, until the elements are orderly.

The heap sorting algorithm is implemented as follows:

voidHeapsort (intA[],intN) {    intI, temp;  for(i=n/2-1; i>=0; i--)//starting from the last non-leaf node, so I was initially n/2-1 .Adjustment (A, I, N-1);//tuning algorithm for call heap     for(i=n-1; i>=1; i--)//to sort n-1{Temp= a[0];//Helper Variable temp is used to hold the value of the root node.a[0] = A[i];//Exchange the root node value with the leaf junction.A[i] =temp; Adjustment (A,0, I-1);//at this point the maximum element is returned, and the remaining n-1 elements call the heap adjustment algorithm, re-adjusted to Dagen    }}

The time complexity of heap sorting is O (nlog2n), and the space complexity is O (1), which is an unstable sorting algorithm.

Sort algorithm--heap sort

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.