归并排序~
除了练习绘图外~算法也不能落下~程序代码:
#include<stdio.h> #include<stdlib.h> #define MAX 255 int R[MAX]={0}; void Merge(int low,int m,int high); void Merge_SortDC(int low,int high); int main() { int i=0; int n=0; puts("Please input total element number of the sequence:"); scanf("%d",&n); if (n<1||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]); Merge_SortDC(1,n); puts("\nThe sequence after merge_sortDC is:"); for (i=1;i<=n;++i) printf("%-4d",R[i]); puts(""); return 0; } void Merge(int low,int m,int high) { /*将两个有序的子文件R[low..m]和R[m+1..high]归并成一个有序的*/ /*子文件R[low..high]*/ int i=low; int j=m+1; int p=0; /*置初始值*/ int *R1=NULL; if ((R1=(int* )malloc((high-low+1)*sizeof (int )))==NULL) { puts("Insufficient memory available!");/*如果分配空间失败*/ exit(0); } while (i<=m&&j<=high)/*两子文件非空时取其小者输出到R1[p]上*/ R1[p++]=(R[i]<=R[j])?R[i++]:R[j++]; while (i<=m)/*若第一个子文件非空,则复制剩余记录到R1中*/ R1[p++]=R[i++]; while (j<=high)/*若第二个子文件非空,则复制剩余记录到R2中*/ R1[p++]=R[j++]; for (p=0,i=low;i<=high;p++,i++) R[i]=R1[p];/*归并完毕后将复制结果放回*/ free(R1); } void Merge_SortDC(int low,int high) { /*用分治法对R[low..high]进行二路归并排序*/ int mid=0; if (low<high) { mid=(low+high)/2;/*分解*/ Merge_SortDC(low,mid);/*递归地对R[low,mid]排序*/ Merge_SortDC(mid+1,high);/*递归地对R[mid+1,high]排序*/ Merge(low,mid,high);/*组合,将两个有序区归并为一个有序区*/ } }