博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
归并排序
阅读量:4217 次
发布时间:2019-05-26

本文共 1077 字,大约阅读时间需要 3 分钟。

//堆排序的原理是遍历数组中的值,得到第一个值为最小,与最后一个值对换,再次遍历,第一个值与倒数第二个值替换,以此类推!#include 
#include
#define MAX 255int R[MAX];void Heapify(int s,int m){ /*对R[1..n]进行堆调整,用temp做暂存单元 */ int j,temp; temp=R[s]; j=2*s; while (j<=m) { if (R[j]>R[j+1]&&j
0;i--) Heapify(i,n);}void Heap_Sort(int n){ /* 对R[1..n]进行堆排序,不妨用R[0]做暂存单元 */ int i; BuildHeap(n); /* 将R[1-n]建成初始堆 */ for(i=n;i>1;i--) { /* 对当前无序区R[1..i]进行堆排序,共做n-1趟。 */ R[0]=R[1]; R[1]=R[i];R[i]=R[0]; /* 将堆顶和堆中最后一个记录交换 */ Heapify(1,i-1); /* 将R[1..i-1]重新调整为堆,仅有R[1]可能违反堆性质 */ } /* end of for */} /* end of Heap_Sort */void main(){ int i,n; //clrscr(); puts("Please input total element number of the sequence:"); scanf("%d",&n); if(n<=0||n>MAX) { printf("n must more than 0 and less than %d.\n",MAX); exit(0); } puts("Please input the elements one by one:"); for(i=1;i<=n;i++) scanf("%d",&R[i]); puts("The sequence you input is:"); for(i=1;i<=n;i++) printf("%4d",R[i]); Heap_Sort(n); puts("\nThe sequence after Big heap_sort is:"); for(i=1;i<=n;i++) printf("%4d",R[i]); puts("\n Press any key to quit..."); getchar();}

转载地址:http://fytmi.baihongyu.com/

你可能感兴趣的文章
USB规范浏览--设备和主机规范
查看>>
男人的品位--我们自己的最求
查看>>
Android (Linux) Suspend流程
查看>>
LINUX时间管理
查看>>
定时器的使用
查看>>
为Android加入busybox工具
查看>>
使用技巧busybox
查看>>
如何查看与/dev/input目录下的event对应的设备
查看>>
bootloader-bootable解析
查看>>
bootloader (LK)&&android lk bootloader中相关修改指南
查看>>
SD卡驱动分析--基于高通平台
查看>>
SD Card 驱动流程分析
查看>>
Linux之debugfs介绍
查看>>
关于sd卡中一些概念的理解
查看>>
sd卡驱动分析之相关硬件操作和总结
查看>>
好的播文
查看>>
linux dd命令解析
查看>>
linux find命令详解
查看>>
S3C2440上touchscreen触摸屏驱动
查看>>
USB History Viewing
查看>>