归并排序之非递归算法时间:2021-10-11 04:13:43总所周知,递归算法是非常低效的;所以就有了非递归算法,相比而言,效率大大的提高了; #include <stdio.h>#include <stdlib.h>void Merge(int *sr, int *di, int left, int mid, int right){ int i = left, j = mid + 1, k = left; while (i <= mid && j <= right) { if (sr[i] < sr[j]) di[k++] = sr[i++]; else di[k++] = sr[j++]; } if (i > mid) while (j <= right) di[k++] = sr[j++]; else if (j > right) while (i <= mid) di[k++] = sr[i++];}void MergeSort(int sr[],int n){printf("/n");int* ds=(int*)malloc(sizeof(int)*n);int begin,center,last,k=1;int l,r,j,e;while(k<n){e=0;begin=0;while(begin<n){l=begin;j=(last=(begin+2*k-1)<n? (begin+2*k-1):(n-1));center=begin+k-1;r=center+1;Merge(sr,ds,l,center,j);begin=last+1;last=last+k;}for(e = 0; e <n ; e++)sr[e] = ds[e]; k=k*2;} free(ds);}int main(){int a[]={26, 5, 37, 1, 61, 11, 59, 15, 48, 19,20,18,20,12,64,43,75,34,65};printf("the pri data is:/n");int total=sizeof(a)/sizeof(int);printf("the total is %3d/n",total);int i;for(i=0;i<total;i++)printf("%3d",a[i]);MergeSort(a,total);printf("/n");for(i=0;i<total;i++)printf("%3d",a[i]);printf("/n");return 0;} 谢谢为本人亲自动手所写;