堆排序heapsort
来源:互联网 发布:c语言常用英语单词 编辑:程序博客网 时间:2024/05/16 07:34
#include<stdio.h>void swap(int& a,int& b) { if(a!=b) { a^=b; b^=a; a^=b; } }void MAX_HEAPIFY(int a[],int length,int i)//a数组第一个存值 { int large=i; if(2*i<length && a[i]<a[2*i]){ large = 2*i; } if(2*i+1<length && a[large]<a[2*i+1]){ large = 2*i+1; } if(large!=i){ swap(a[i],a[large]); MAX_HEAPIFY(a,length,large); }}void BUILD_MAX_HEAP(int a[],int length)//将每个非叶子节点从下向上调用MAX_HEAPIFY { for(int i = length/2-1;i>=0;i--) { MAX_HEAPIFY(a,length,i); }}void HEAP_SORT(int a[],int length){ BUILD_MAX_HEAP(a,length); for(int i = length-1; i>0;i--) { swap(a[0],a[length-1]); MAX_HEAPIFY(a,--length,0);//保证最大堆的性质 }}int main(){ int a[10] = {16,4,10,14,7,9,3,2,8,1}; HEAP_SORT(a,10); for(int i=0;i<10;i++) { printf("%d ",a[i]); }}
0 0
- 堆排序(HeapSort)
- 堆排序 heapSort
- HeapSort 堆排序
- C++ 堆排序 (HeapSort)
- 堆排序heapsort
- 堆排序(HeapSort)
- HeapSort(堆排序 C++)
- 堆排序(Heapsort)
- 堆排序 (Heapsort)
- 堆排序 Heapsort
- 堆排序(heapsort)
- 堆排序(HeapSort)
- 堆排序(heapsort)
- 堆排序(heapsort)
- heapsort堆排序
- 堆排序heapsort
- 堆排序 HeapSort
- 堆排序(Heapsort)
- centos下搭建redis集群
- fifo_read0
- 搜索引擎原理及使用
- Linux运维工程师需要掌握什么才能胜任工作呢
- 整理一下RecyclerView好的开源框架
- 堆排序heapsort
- JVM性能调优监控命令
- 瞌睡的大白
- 一个编程初学者的成长历程
- 网易的一道面试题
- DOM
- 排序之归并排序
- 至自己
- 碉堡了Ubuntu Core Linux支持 TS-4900 物联网开发板