Introduction to algorithms-sorting (3) Heap sorting

Source: Internet
Author: User

Introduction to algorithms-sorting (3) Heap sorting
Directory 1, heap sorting Introduction 2, heap sorting Example 3, c ++ complete code 4, reference content 1, heap sorting Introduction 1.1, what heap is a complete binary tree, (If a node is set to I and the root node starts from 0, the left child node is 2 * I + 1, and the right child node is 2 * I + 2 ), any non-leaf node in the heap satisfies the following requirements: key [I]> = Key [2 * I + 1] & Key [I]> = Key [2 * I + 2] or Key [I] <= Key [2 * I + 1] & Key [I] <= Key [2 * I + 2], that is to say, for any parent node, either greater than or equal to the child node or less than or equal to the child node, the heap of Key [I]> = Key [2 * I + 1] & Key [I]> = Key [2 * I + 2] is called a big top heap; key [I] <= Key [2 * I + 1] & Key [I] <= Key [2 * I + 2] is called a small top heap; from the above properties, we can see that the keyword of the heap top of the big top stack must be the largest among all keywords. The keyword of the heap top of the small top stack is the minimum 1.2 of all keywords. Heap sorting means sorting based on the heap. How can we sort it? [1] First, create a large top heap from the array to be sorted (here, we will explain how to create a large top heap, as described below), so that we can see from the nature of the Large top heap, the maximum number of heap headers. [2] Then, swap the heap top key [0] and the last heap element Key [n-1], key [0] <-> Key [n-1], so that the maximum number is placed in the final Key [n-1] of the heap. Because the elements are exchanged ~ (N-2) nodes may not meet the requirements of the Big Top heap, so then the heap Key [0] -- Key [N-2] is adjusted to the Big Top heap; [3] change the size of the dashboard [0 ~ (N-2)], swap heap top Key [0] and heap last element Key [N-2], Key [0] <-> Key [N-2], so that, key [N-2], Key [n-1] The maximum two numbers have been sorted; re-adjusted [0 ~ (N-3)] for the Big Top heap; keep repeating this process until the entire sorting process is completed how to build the Big Top heap? [1] First, start from the largest non-leaf node (set as Key [I]) and adjust it, if the values of keys [2 * I + 1] and keys [2 * I + 2] of child nodes are greater than those of Key [I, switch the Key [I] of this node to a large node in the subnode. Then, the child nodes after the switch are used as the current node and adjusted down until the leaf node is encountered. [2] after the maximum non-leaf node Key [I] is adjusted, adjust the node Key [I-1] down until the leaf node appears; repeat this process until the root node is adjusted down; below is an example of creating a large top heap: the array to be sorted is [5 16 3 20 17 4] 2. The figure above shows how to create a large top heap. After creating a large top heap, you can sort it, the following is an example of heap sorting: 3. c ++ complete code Sort. h (implementation of heap sorting function) Copy code 1 # ifndef SORT_HH 2 # define SORT_HH 3 template <typename T> 4 class Sort 5 {6 public: 7 void Swap (T & m, T & n); // exchange data 8 void print_element (vector <T> A); // print array 9 void HeapSort (vector <T> & A, bool IsRecurFlag); 10 private: 11 void HeapAdjust (vector <T> & A, int I, int n ); // non-recursive heap adjustment 12 void HeapAdjust_Recursive (vector <T> & A, int I, int n); // recursively adjusts heap 13 void BuildBigHeap (vector <T> &, bool IsRecurFlag); // build a large top heap 14}; 15 template <typename T> // exchange data 16 void Sort <T>: Swap (T & m, T & n) 17 {18 T tmp; 19 tmp = m; 20 m = n; 21 n = tmp; 22} 23 // adjust from the I node, n indicates the total number of nodes. The subnodes that start from 0 calculate the I node are 2 * I + 1, 2 * I + 2 24 template <typename T> 25 void Sort <T >:: HeapAdjust (vector <T> & A, int I, int n) 26 {27 T temp = A [I]; 28 int j = 2 * I + 1; // subscript starting from 0 29 while (j <n) // non-recursive 30 {31 if (j + 1 <n & A [j + 1]> A [j]) // find the minimum value of the child node 32 j ++; 33 if (A [j] <= temp) // if the child node is not greater than the parent node, the adjustment ends with 34 break; 35 36 A [I] = A [j]; // the maximum value of the child node is greater than that of the parent node, and the value is assigned to the parent node; 37 I = j; 38 j = 2 * I + 1; // The subscript starts from 0 39} 40 A [I] = temp; // The adjustment ends 41} 42 // The adjustment starts from the I node, n indicates the total number of nodes. From 0, the subnode for I node calculation is 2 * I + 1, and 2 * I + 2 43 template <typename T> 44 void Sort <T >:: HeapAdjust_Recursive (vector <T> & A, int I, int n) // recursively adjust heap 45 {46 int lchild = 2 * I + 1; 47 int rchild = 2 * I + 2; 48 int max = I; 49 if (I <= (n-1)/2) // adjust 50 {51 if (lchild <n & A [lchild]> A [max]) 52 {53 max = lchild from the maximum non-leaf node; 54} 55 if (rchild <n & A [rchild]> A [max]) 56 {57 max = rchild; 58} 59 if (max! = I) // The value of the subnode is greater than that of the parent node. Adjust 60 {61 Swap (A [I], A [max]); 62 HeapAdjust_Recursive (A, max, n ); // recursion 63} 64} 65} 66 // initially build a large top heap from the array to be sorted 67 // The array starts from 0, heap building from p to q 68 template <typename T> 69 void Sort <T >:: BuildBigHeap (vector <T> & A, bool IsRecurFlag) 70 {71 int n =. size (); 72 for (int I = (n-1)/2; I> = 0; I --) // start from the maximum non-leaf node 73 {74 if (IsRecurFlag) 75 HeapAdjust_Recursive (A, I, n); 76 else 77 HeapAdjust (A, I, n); 78} 79} 80 // heap sorting, first, create A large top heap 81 template <typename T> 82 void Sort <T>: HeapSort (vector <T> & A, bool IsRecurFlag) 83 {84 int len =. size (); 85 BuildBigHeap (A, IsRecurFlag); // build large top heap 86 for (int I = len-1; I> = 0; I --) 87 {88 Swap (A [0], A [I]); // unordered heap first and last Swap 89 if (IsRecurFlag) // recursion 90 HeapAdjust_Recursive (, 0, I); 91 else // non-recursive 92 HeapAdjust (A, 0, I ); // adjust to the large top heap 93} 94} 95 96 template <typename T> // print the array 97 void Sort <T >:: print_element (vector <T>) 98 {99 int len =. size (); 100 for (int I = 0; I <len; I ++) 101 {102 std: cout <A [I] <""; 103} 104 std: cout <std: endl; 105} 106 # endif copy code Sort. cpp (main test function) Copy code 1 # include <iostream> 2 # include <vector> 3 using namespace std; 4 # include "Sort. h "5 6 int main () 7 {8 Sort <int> sort1; 9 int a [] = {3,178, 23,134, 1,-27,1345, 80 }; 10 vector <int> vec_int1 (a, a + 10); 11 cout <"source array:"; 12 sort1.print _ element (vec_int1 ); 13 cout <"non-recursive sorting:"; 14 sort1.HeapSort (vec_int1, false); 15 sort1.print _ element (vec_int1); 16 17 vector <int> vec_int2 (, a + 10); 18 cout <"recursive sorting:"; 19 sort1.HeapSort (vec_int2, true); 20 sort1.print _ element (vec_int2); 21 system ("PAUSE "); 22 return 0;

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.