//插入排序 //直接插入排序 二分插入排序 2-路插入排序 表插入排序 希尔排序 #include<stdio.h> #include<stdlib.h> void directsort(int *a,int n) //直接插入排序 { int i,j; int t; for(i=2;i<=n;i++) { t=a[i]; for(j=i-1;a[j]>t&&j>0;j--) a[j+1]=a[j]; a[j+1]=t; } } void bisort(int *a,int n) //二分插入排序 { int i,j,t,m; int low,high; for(i=2;i<=n;i++) { t=a[i]; low=1; high=i-1; while(low<=high) { m=(low+high)/2; if(a[m]>a[i]) high=m-1; else low=m+1; } for(j=i-1;j>=low;j--) a[j+1]=a[j]; a[j+1]=t; } } void swap(int *a,int i,int j) { int t; t=a[i]; a[i]=a[j]; a[j]=t; } void biinsert(int *a,int n)//2-路插入排序 { int d[n+1]; int i,j,k,t,m; int first,final,high,low; first=final=1; d[1]=a[1]; for(i=2;i<=n;i++) { if(a[i]>d[1]) { low=1; high=final; while(low<=high) { m=(low+high)/2; if(a[i]>d[m]) low=m+1; else high=m-1; } for(j=final;j>=low;j--) d[j+1]=d[j]; d[low]=a[i]; final++; } else { if(first==1) { first=n; d[n]=a[i]; } else { low=first; high=n; while(low<=high) { m=(low+high)/2; if(a[i]>d[m]) low=m+1; else high=m-1; } for(j=first;j<=high;j++) a[j-1]=a[j]; a[high]=a[i]; first--; } } } for(i=first,j=1;i<=n;i++,j++) a[j]=d[i]; for(i=1;i<=final;i++,j++) a[j]=d[i]; } void shellinsert(int *a,int n,int dk) //希尔排序的插入操作 { int i,j,t; for(i=dk+1;i<=n;i++) { t=a[i]; for(j=i-dk;j>0&&a[j]>t;j-=dk) a[j+dk]=a[j]; a[j+dk]=t; } } void shellsort(int *a,int n) //希尔排序 { int i; for(i=1;i<=5;i++) shellinsert(a,10,5-i); } void output(int *a,int n) { int i; for(i=1;i<=n;i++) printf("%d ",a[i]); printf("/n"); } int main() { int a[]={0,12,78,895,24,158,369,58,621,1475,2}; output(a,10); shellsort(a,10); output(a,10); system("pause"); }