1、直接插入排序基本算法#include#include#includeconst int n=100000;typedef structint key;RedType;typedef struct RedType *r; /rn+1;int length;SqList;int random();void InsertSort(SqList void main() SqList L;L.r = new RedTypen+1;L.length=n;for(int i=1;i=0) x=x1;else x=x1+M;return x;void InsertSort(SqList i#include#i
2、ncludeconst int n=100000;typedef structint key;RedType;typedef struct RedType *r; /rn+1;int length;SqList;int random();void InsertSort(SqList void main() SqList L;L.r = new RedTypen+1;L.length=n;for(int i=1;i=0) x=x1;else x=x1+M;return x;void InsertSort(SqList /对顺序表L作折半插入排序。for(int i=2;i=high+1;-j)L
3、.rj+1=L.rj; /记录后移L.rhigh+1=L.r0; /插入 /for/InsertSort改进2表插入排序#include#include#includeconst int n=100000;const int MAXINT=2147483647;typedef struct int key; /rn+1;int next;SLNode;typedef struct SLNode *r;int length;SLinkList;int random();void LInsertionSort (SLinkList void Arrange(SLinkList void main(
4、) SLinkList L;L.r = new SLNoden+1;L.length=n;for(int i=1;i=0) x=x1;else x=x1+M;return x;void LInsertionSort (SLinkList SL.r0.key = MAXINT ; SL.r0.next = 1; SL.r1.next = 0; for ( i=2; i#include#include#include#includeconst int n=1000000;typedef structint key;RedType;typedef struct RedType *r; /rn+1;i
5、nt length;SqList;int random();int partition(SqList void QSort(SqList void main() int t,m;SqList L;L.r = new RedTypen+1;L.length=n;for(int i=1;i=0) x=x1;else x=x1+M;return x;int partition(SqList /交换顺序表L中子表rlowhigh的记录,枢轴记录到位,并返回其所在位置,此时在它之前(后) 的/记录均不大(小) 于它.L.r0=L.rlow; /用子表的第一个记录作枢轴记录pivotkey=L.rlow.
6、key; /枢轴记录关键字while(low=pivotkey)-high;L.rlow=L.rhigh; /将枢轴记录小的记录移到低端while(low#include#include#include#includeconst int n=1000000;typedef structint key;RedType;typedef struct RedType *r; /rn+1;int length;SqList;int random();void Merge(SqList void MergePass(SqList void MergeSort(SqList void main() / i
7、nt m;/m=log10(double(n+1)/log10(double(2);SqList L;SqList L1;L.r = new RedTypen+1;L1.r=new RedTypen+1;L.length=n;for(int i=1;i=0) x=x1;else x=x1+M;return x;void Merge(SqList i=low;j=mid+1;k=low;while(i=midelse R1.rk+=R.rj+;while(i=mid) R1.rk+=R.ri+;while(j=high) R1.rk+=R.rj+;void MergePass(SqList i=
8、1; /i指第一对子表的起始点while(i+2*len-1=m) /归并长度为len的两个子表Merge(R,R1,i,i+len-1,i+2*len-1);i=i+2*len; /指向下一对子表起始点if(i+len-1m) /剩下两个子表,其中一个长度小于lenMerge(R,R1,i,i+len-1,m);else /子表个数为奇数 ,剩一段for(j=i;j=m;j+) /将最后一个表复制到R1中R1.rj=R.rj;void MergeSort(SqList len=1;while(lenh)MergePass(R,R1,h,len);len=len*2; /一趟归并,结果在R1 中MergePass(R1,R,h,len);len=len*2; /再次归并,结果在R 中