#include <stdio.h>
//折半插入排序
void BInsertSort(int a[], int n)
{
int low ;
int high ;
int mid =0;
int temp ;
for(int i =1;i<n;i++){
low =0;
temp =a[i];//先把当前要比较的值存起来
high = i-1;
while(low<=high){
mid = (low+high/2);
if (a[mid]>temp) {
high=mid-1;
}else{
low=mid+1;
}
}
for(int j=i;j>low;j--){//把low后面的元素后移
a[j] = a[j-1];
}
a[low] =temp;
}
}
int main(){
int a[50] = {2,3,4,1};
BInsertSort(a,4);
for(int i=0;i<4;i++){
printf(" %d",a[i]);
}
return 0;
}
王道第八章排序算法3-折半插入排序算法C语言
最新推荐文章于 2023-04-18 17:27:20 发布