public class BInsertSort {
public static void main(String[] args) {
int[] arr = { 49, 38, 65, 97, 76, 13, 27, 49 };
sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void sort(int[] arr){
for(int i =1;i<arr.length;i++){
int temp = arr[i];
int low = 0;
int high = i-1;
while(low <=high){//在[low..high]中折半查找有序插入的位置
int m = (low + high)/2;
if(temp < arr[m]){
high = m-1;
}
else{
low = m+1;
}
}
for(int j = i-1;j>=high+1;j--){//记录后移
arr[j+1] =arr[j];
}
arr[high + 1] =temp;//插入
}
}
}
折半插入排序
最新推荐文章于 2020-03-29 15:41:48 发布