标签:class com using UI sort 堆排 div bsp algorithm
具体解析可参见:http://bubkoo.com/2014/01/14/sort-algorithm/heap-sort/
#include<bits/stdc++.h> using namespace std; void heap(int *a, int index, int Size){ int left = 2*index + 1; int right = 2*index + 2; int iMAX = index; if(right < Size && a[iMAX] < a[left]) iMAX = left; if(right < Size && a[iMAX] < a[right]) iMAX = right; if(iMAX != index){ swap(a[index], a[iMAX]); heap(a, iMAX, Size); } } void buildHeap(int *a, int len){ int index = (len/2)-1; for(int i = index; i >= 0; i--){ heap(a, i, len); } } void heapSort(int *a, int len){ buildHeap(a, len); for(int i = len; i >= 1; i--){ swap(a[0], a[i-1]); heap(a, 0, i-1); } } int main(){ int a[10] = {16, 10, 4, 14, 7, 9, 3, 2, 8, 1}; heapSort(a, 10); for(int i = 0; i < 10; i++){ cout << a[i] << ‘ ‘; } }
标签:class com using UI sort 堆排 div bsp algorithm
原文地址:http://www.cnblogs.com/Vincent-Bryan/p/6498737.html