Heap Sort Implementation

Source: Internet
Author: User

1. Heap Sorting algorithm Descriptive description:

(1) Definition

n a keyword sequence kl,k2,...,kn called (Heap). When and only if the sequence satisfies for example 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 little Gan. Dagen is replaced by the >= number. K (i) corresponds to a non-leaf node of a two-fork tree, and K (2i) is the left Dial hand node. K (2i+1) is the right child node2) If the vector stored in this sequence R[1..N] is considered to be a tree Totally binary Treeof theStorage structure, the heap is essentially a fully binary tree that satisfies the following properties, such as: the keyword of any non-leaf node in the tree are not greater than (or not less than) the keyword of their children (if present) nodes.

(2) The basic idea of sorting with Dagen

① the initial file R[1..N] to build a large root heap. This heap is the initial unordered zone ② then keyword the largest record r[1] (that is, the top of the heap) 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 satisfies R[1..n-1].keys≤r[n].key③ because the new root r[1] may violate the heap nature, so the current unordered area r[1..n-1] should be adjusted to the heap.

Then again, r[1..n-1] keyword the largest record r[1] and the last record of the interval r[n-1] Exchange. This results in a new disordered zone r[1..n-2] and an ordered area r[n-1. N]. And still satisfies the relationship r[1..n-2].keys≤r[n-1. N].keys, the same as to adjust r[1..n-2] to a heap.

...... Until the unordered zone has only one element. (3) Basic operation of Dagen sorting algorithm: ① build heap. Building the heap is the process of constantly adjusting the heap, starting from LEN/2 and adjusting to the first node, where Len is the number of elements in the heap. The process of building a heap is a linear process. The process of tuning the heap has been called from LEN/2 to 0. Equivalent to O (H1) +o (H2) ... +o (HLEN/2) h represents the depth of the node. LEN/2 represents the number of nodes, which is a summation process, and the result is a linear O (n). ② adjustment heap: The tuning heap is used during the build heap. It is also used during the heap sequencing process. The idea is to use a node I and its child node left (i), right (i), to select the three largest (or least) of them. Assume that the maximum (small) value is not a node I but one of its child nodes. Over there the interaction node I and the 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 the 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 based on elements.

The

then takes the root node of the heap out (usually in exchange with the last node). The process of continuing the heap adjustment with the previous len-1 node is then taken out of the root node 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, called n-1 Times. So the time complexity of heap sorting is O (NLGN). 2, heap storage

The index of the left and right sub-nodes of the

I node is 2*i+1 and 2*i+2
3, Heap Sorting algorithm implementation (max 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)//From Start index node start adjustment {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 the node at the start index. Interchange {TMP = Thearray[fatherindex];thearray[fatherindex] = Thearray[rightindex];thearray[rightindex] = tmp;}} if (Leftindex < n)//start index node exists left child node, but does not necessarily exist right child node {if (Thearray[fatherindex] < Thearray[leftindex])//Right child node large To the 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); 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              Out recursion 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). Impression: When you start to build a heap, you think that you need to build a binary tree assist, in fact the so-called heap (that is, the total binary tree) is only a logical structure. The real physical structure is still the sequential array. In the process of building a large heap or a small heap, all are based on sequential arrays, and logically operate a completely binary tree, so there is absolutely no need to establish an auxiliary completely binary tree.

Heap Sort Implementation

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.