今天刚刚学习堆和堆排,先手抄了一遍算法书,再自己实现一遍,作为巩固。
update : 早上起床把小根堆写了一遍 = 。 =
1 /* 小根堆 */ 2 #include3 #include 4 using namespace std; 5 void downAdjust(int to_adjust,int bound,vector<int> & heap){ 6 int i = to_adjust, j = i * 2; 7 while(j <= bound){ 8 if(j + 1 <= bound and heap[j] > heap[j+1]){ 9 j = j + 1; 10 } 11 if(heap[i] > heap[j]){ 12 swap(heap[i], heap[j]); 13 i = j; 14 j = i*2; 15 }else break; 16 } 17 } 18 void upAdjust(int to_adjust,vector<int> & heap){ 19 int i = to_adjust, j = i/2; 20 while(j >= 1){ 21 if(heap[i] < heap[j]){ 22 swap(heap[i], heap[j]); 23 i = j; 24 j = i/2; 25 }else break; 26 } 27 } 28 void insert(int v,vector<int> & heap){ 29 heap.push_back(v); 30 int s = (int)heap.size()-1; 31 upAdjust(s, heap); 32 } 33 void pop(vector<int>& heap){ 34 swap(heap[1], heap.back()); 35 heap.pop_back(); 36 int s = (int)heap.size() - 1; 37 downAdjust(1,s,heap); 38 } 39 void create_heap(vector<int>&heap){ 40 int s = (int)heap.size()-1; 41 for(int i = s/2; i >= 1; i--){ 42 downAdjust(i, s, heap); 43 } 44 } 45 void heapSort(vector<int>& heap){ 46 create_heap(heap); 47 for(int s = (int)heap.size()-1; s > 1; s--){ 48 swap(heap[1], heap[s]); 49 downAdjust(1,s-1, heap); 50 } 51 } 52 int main(){ 53 vector<int> test = {-1,2,4,1,5,23,56,75,32,134,5435,31,1,3412,45,9}; 54 heapSort(test); 55 for(vector<int>::iterator it = test.begin(); it not_eq test.end(); it++)cout<<*it<<" "; 56 return 0; 57 }
- - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1 /* 大根堆 */ 2 #include3 #include 4 #define Maxsize 1000 + 1 5 using namespace std; 6 void downAdjust(int low,int high,vector<int>& Heap){ 7 int i = low, j = i * 2; // i 是将要下调的结点 , j 是i的左结点 8 while( j <= high ){ // 存在孩子结点 9 if(j + 1 <= high and Heap[j] < Heap[j+1]){ 10 j = j + 1; 11 } 12 if(Heap[i] < Heap[j]){ 13 swap(Heap[i],Heap[j]); 14 i = j; 15 j = i*2; 16 }else break; 17 } 18 } 19 void creatHeap(vector<int>& Heap){ 20 int s = (int)Heap.size() - 1; 21 for(int i = s/2; i >= 1; i--){ 22 downAdjust(i, s, Heap); 23 // 不需要考虑最底层的结点, 直接从次底层开始,从下向上遍历调整。 24 // 为什么不从堆顶向下调整呢? 25 //如果从堆顶调整, 对于一个结点,在调整其子结点的步骤中可能会破坏该节点的局部大根堆结构,导致建立堆错误。 26 } 27 } 28 void pop(vector<int>& Heap){ 29 int s = (int)Heap.size() - 1; 30 swap(Heap[1], Heap[s]); 31 Heap.pop_back(); 32 downAdjust(1, s-1, Heap); 33 } 34 void upAdjust(int high,vector<int>& Heap){ 35 int i = high, j = high/2; 36 while( j >= 1){ 37 if(Heap[i] > Heap[j]){ 38 swap(Heap[i], Heap[j]); 39 i = j; 40 j = i/2; 41 }else break; 42 } 43 } 44 void insert(vector<int>&Heap,int value){ 45 Heap.push_back(value); 46 int s = (int)Heap.size() - 1; 47 upAdjust(s, Heap); 48 } 49 void HeapSort(vector<int>& Heap){ 50 creatHeap(Heap); 51 for(int s = (int)Heap.size() - 1; s > 1; s--){ 52 swap(Heap[1], Heap[s]); 53 downAdjust(1, s-1, Heap); 54 } 55 } 56 int main(){ 57 vector<int> test = {0,1,314,41,2,1,3,14,14,3,11313,134}; // 0 是占位符 58 HeapSort(test); 59 for(vector<int> :: iterator it = test.begin() + 1; it not_eq test.end(); it++)cout<<*it<<" "; 60 return 0; 61 }