标签:apr col 堆排序 void bsp dma index 条件 swa
1 inline int heapParent(int index) 2 { 3 if(index & 1) 4 return index >> 1; 5 return (index >> 1) - 1; 6 } 7 8 inline int heapLeft(int index) 9 { 10 return (index << 1) + 1; 11 } 12 13 inline int heapRight(int index) 14 { 15 return (index << 1) + 2; 16 } 17 18 19 void Solution::maxHeapify(vector<int>& vec, int index, int size) 20 { 21 int largest; 22 int lchild = heapLeft(index); 23 int rchild = heapRight(index); 24 if(lchild < size && vec[lchild] > vec[index]) 25 largest = lchild; 26 else largest = index; 27 28 if(rchild < size && vec[rchild] > vec[largest]) 29 largest = rchild; 30 31 if(largest != index) 32 { 33 swap(vec[index], vec[largest]); 34 maxHeapify(vec, largest, size); 35 } 36 } 37 38 void Solution::buildMaxHeap(vector<int>& vec, int size) 39 { 40 for(int i = size / 2; i >= 0; i--) 41 { 42 maxHeapify(vec, i, size); 43 } 44 } 45 46 void Solution::heapSort(vector<int>& vec) 47 { 48 int size = vec.size(); 49 buildMaxHeap(vec, size); 50 for(int i = size - 1; i > 0; i--) 51 { 52 swap(vec[i], vec[0]); 53 size--; 54 buildMaxHeap(vec, size); 55 } 56 }
一些临界条件设置得有点随意。。。
标签:apr col 堆排序 void bsp dma index 条件 swa
原文地址:https://www.cnblogs.com/cccv/p/algorithm_004.html