Heap Sort Implementation

Source: Internet
Author: User

1, Heap sorting algorithm Description:

(1) Definition

n a keyword sequence kl,k2,...,kn called (heap), when and only if the sequence satisfies the following properties (for short, heap properties): 1) ki<=k (2i) and Ki<=k (2i+1) (1≤I≤N/2), of course, this is the small Gan, Dagen is replaced by the >= number. K (i) corresponds to a non-leaf node of a two-fork tree, K (2i) is a left dial hand node, K (2i+1) is the right child node.2) If the vector stored in this sequence R[1..N] is considered to be a tree Complete binary Treeof theStorage structure, the heap is essentially a complete binary tree that satisfies the following properties: The keywords of any non-leaf node in the tree are not greater than (or not less than) the keywords of their left and right child (if any) nodes. (2) The basic idea of sorting with Dagen

① the initial file R[1..N] into a large heap, this heap is the initial unordered area ② again the largest record of the keyword R[1] (that is, the heap top) and the last record of the unordered zone R[n] exchange, resulting in a new unordered area r[1..n-1] and ordered area R[n], and meet r[1..n-1]. Keys≤r[n].key③ the current unordered area r[1..n-1] should be adjusted to a heap because the new root r[1] may violate the heap nature because of the exchange. Then again, the largest record of the r[1..n-1] r[1] and the last record of the interval r[n-1] exchange, resulting in a new unordered area R[1..n-2] and ordered area r[n-1. N], and still satisfies the relationship r[1..n-2].keys≤r[n-1. N].keys, R[1..n-2] will also be adjusted to the heap. ...... Until there is only one element in the unordered area. (3) Basic operation of Dagen sorting algorithm: ① build heap, build heap is the process of constantly adjusting the heap, starting from LEN/2 to adjust, until the first node, where Len is the number of elements in the heap. The process of building the heap is a linear process, from LEN/2 to 0 has been called to adjust the heap process, equivalent to O (H1) +o (H2) ... +o (HLEN/2) where h represents the depth of the node, LEN/2 represents the number of nodes, this is a summation process, the result is a linear O (n). ② adjustment heap: The tuning heap is used during the build heap and is used during the heap sequencing process. The idea is to compare node I and its child node left (i), right (i), select the maximum (or minimum) of the three, if the maximum (small) value is not node I but it's a child node, over there the interaction node I and that node, and then call the tuning heap process, which is a recursive process. The process time complexity of adjusting the heap is related to the depth of the heap, which is a LGN operation because it is adjusted along the depth direction. ③ heap Sequencing: Heap sequencing is done using the two processes above. The first is to build the heap from elements. The root node of the heap is then taken out (typically swapped with the last node), and the previous len-1 node continues the heap adjustment process before the root node is removed so that all nodes are removed. The time complexity of the heap sequencing process is O (NLGN). Because the time complexity of building the heap is O (n) (called once), the time complexity of the adjustment heap is LGN, the n-1 times are called, so the time complexity of the heap ordering is O (NLGN). 2. Heap Storagegenerally use an array to represent the heap, the Joghen node exists ordinal 0, I node of the parent node subscript is (i-1)/2. The index of the left and right sub-nodes of the I node is2*i+1and the2*i+2.
3, Heap sorting algorithm implementation (maximum heap)
#include <iostream>void printArray (int thearray[], int n) {for (int i = 0; i < n; i++) {std::cout << thearray[ I] << "";} Std::cout << Std::endl;} void adjustheap (int thearray[], int n, int start)//Start adjustment from the node at start index {if (Start < 0 | | start >= n) return;int Fatherin        Dex;int leftindex;int Rightindex;int tmp;fatherindex = Start;leftindex = 2 * fatherindex + 1;             Index of the left child of the node at start index Rightindex = leftindex + 1; The right child's index of the node at the start index if (Rightindex < n) the node at the//start index has a right child node. {if (Thearray[fatherindex] < Thearray[rightindex])//Right child node is greater than node at Start index, interchange {TMP = THEARRAY[FATHERINDEX];THEARRAY[FA Therindex] = Thearray[rightindex];thearray[rightindex] = tmp; }}if (Leftindex < N)//start node at the index there is a left child node, but there is not necessarily a right child node {if (Thearray[fatherindex] < Thearray[leftindex])//Right child knot Point greater than left child node tree, swap {tmp = Thearray[fatherindex];thearray[fatherindex] = Thearray[leftindex];thearray[leftindex] = tmp;}}         Adjustheap (TheArray, N, start-1); Re-adjust the start cable recursivelyThe previous node in the lead}void constructheap (int thearray[], int n) {int start = N/2; Adjustheap (TheArray, N, start); Starting from LENGTH/2 adjust}void heapsort (int thearray[], int n) {if (n = = 1)//When there is only one element left in the unordered sequence, jump directly              Recursive return;int length = N;//printarray (thearray, length);                    Prints out the array arrangement after each build of the maximum heap//swaps the heap top element of the largest heap with the end element of the heap and extracts the element as the sorted array element int tmp = thearray[0]; Thearray[0] = thearray[length-1];thearray[length-1] = tmp;                 Once again the exchange of the heap is adjusted so as to satisfy the maximum heap--length;   Unsorted sequence length minus one int start = LENGTH/2;             Adjustheap (thearray, length, start); Heapsort (thearray, length); Recursive heap sorting}int main (int argc, char *argv[]) {int myarray[] = {5,90,28,4,88,58,38,18,19,20};int length = sizeof (MyArray)/ sizeof (myarray[0]); Constructheap (myArray, length); Heapsort (MyArray, length);p rintarray (myArray, length); return 0;}

4, the time complexity of the heap and the complexity of spacethe best, worst, and average time complexity of the heap is O (nlogn).the space complexity of the heap is O (1).thoughts: When the first implementation of the heap, thinking that need to establish a binary tree assist, in fact, the so-called heap (that is, a complete binary tree) is only a logical structure, the real physical structure or the order of the array, in the process of building a large heap or small heap, are based on the sequence array, and logically operate a completely binary Therefore, there is no need to establish an auxiliary complete binary tree.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Heap Sort Implementation

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.