#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void InsertSort(int A[],int n){
int j,i,temp,low,high,mid;
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){ //保证插入排序稳定性mid=temp时也往后移
low=mid+1;
}
else
high=mid-1;
}
for(j=i-1;j>=low;j--){
A[j+1]=A[j];
}
A[j+1]=temp;
}
}
void Printf(int A[],int n){
for(int i=0;i<n;i++){
printf("%d ",A[i]);
}
}
int main(){
int A[11]={0,7,8,5,1,4,2,3,15,7,6};
int n=11;
InsertSort(A,n);
Printf(A,n);
return 0;
}
直接插入排序(折半查找法)
最新推荐文章于 2022-10-17 21:41:03 发布