直接插入排序 0(n^2)
void DirectInsertSort(int A[],int n){
int i,j,temp;
for(i=1;i<n;i++){
if(A[i]<A[i-1]){
temp=A[i];
for(j=i-1;j>=0 && A[j]>temp;j--){
A[j+1]=A[j];
}
A[j+1]=temp;
}
}
}
折半插入排序 0(n^2)
void BinaryInsertSort(int A[],int n){
int i,j,low,high,mid,temp;
for(i=1;i<n;i++){
temp=A[i];
low=0;high=i-1;
while(low<=high){
mid=(low+high)/2;
if(A[mid]<temp){
low=mid+1;
}else{
high=mid-1;
}
}
for(j=i-1;j>low;j--){
A[j+1]=A[j];
}
A[low]=temp;
}
}
希尔排序 0(n ^1.3 - n ^2)
void ShellSort(int A[],int n){
int i,j,d,temp;
for(d=n/2;d>0;d=d/2){
for(i=d;i<n;i++){
if(A[i]>A[i+d]){
temp=A[i];
for(j=i-d;j>=0&&A[j]>temp;j=j-d){
A[j+d]=A[j];
}
A[j+d]=temp;
}
}
}
}