首页 > 代码库 > Heap Sort
Heap Sort
#include <iostream.h> #define N 8 int a[]={0,39,21,40,92,29,11,32,9}; void Adjust(int i,int last) { int k=2*i; int t=a[i],tag=1; while(k<=last&&tag) { if(k<last&& a[k]>a[k+1]) k++; if(t>a[k]) { a[i]=a[k]; i=k; k=2*i; } else tag=0; } a[i]=t; } void disp( int n) { for (int i=0;i<=N;i++) cout<<a[i]<<‘\t‘; cout<<endl; } void HeapSort(int n) { int t; for(int i=n/2;i>=1;i--)//build heap; { Adjust(i,n); } while(n>1) { disp(8); t=a[1];//交换堆的第一与最后元素 a[1]=a[n]; a[n]=t; // disp(8);cout<<n; Adjust(1,n-1);//交换后第n个元素就不要考虑了 cout<<endl; n--; } } void main() { HeapSort(8); disp(8); }
Heap Sort
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。