HeapSort(堆排序)
2015-03-28 16:37
483 查看
#include<iostream> using namespace std; void exch( int* x, int* y ) { int temp = *x; *x =*y; *y = temp; } void HeapSort( int* array, int n )//array[0]不存放元素 { for( int i = (n-1)/2; i > 0; i-- ) { if( array[2*i] > array[i] ) exch( &array[2*i], &array[i] ); if( array[2*i+1] > array[i] && (2*i+1) < n) exch( &array[2*i+1], &array[i] ); } } void OutPut( int* array, int n ) { for( int i = 1; i < n; i++ ) { cout<<array[i]<<" "; } cout<<endl; } int main() { const int N = 12; int a = {0,13,2,3,4,16,336,3,8,9,10,11}; int size = N; OutPut( a, N); while( size > 1 ) { HeapSort( a, size ); exch( &a[1], &a[size-1] ); size--; } OutPut( a, N); return 0; }
相关文章推荐
- 堆排序(Heap Sort)
- 堆排序(Heap Sort)
- PAT 1098. Insertion or Heap Sort (25) 堆排序和插入排序
- [硕.Love Python] HeapSort(堆排序)
- heapSort - 堆排序 / 二叉堆
- 堆排序(Heap Sort)
- 经典排序算法 - 堆排序Heap sort
- 堆排序(heap-sort)小结
- 堆排序(heapsort)
- js算法:heap sort 使用堆排序
- 堆排序(HeapSort)
- 堆(heap)、堆排序(heapSort)、优先级队列
- 堆排序(Heap Sort)
- C++、C#、java算法学习日记07----堆排序(HeapSort)
- 堆排序 (Heap Sort)
- 【算法导论学习-013】堆排序(Heapsort)
- 排序算法之堆排序(HeapSort)
- 堆排序HeapSort(Java)
- 排序算法之堆排序(Heapsort)解析
- HeapSort(堆排序)