package sort;
/**
* 直接插入排序
*
* @author lin
*
*/
public class Select {
static int a[] = {5,3,6,7,3,2,9,6,3,6};
public static void main(String[] args) {
sort(a);
print(a);
}
public static void sort(int a[]){
for(int i = 1; i < a.length ; i++){
//当整体向后移时,保证a[i]的值不会丢失
int tmp = a[i];
int low = 0;
int high = i -1;
while(low <= high){
int mid = (low + high)/2;
if(tmp > a[mid]){
low = mid + 1;
}else{
high = mid -1;
}
}
//将low到i处的所有元素向后整体移一位
for(int j = i; j > low; j--){
a[j] = a[j-1];
}
a[low] = tmp;
}
}
public static void print(int a[]){
for(int i = 0; i < a.length; i ++)
System.out.format("%d ", a[i]);
}
}
插入排序--折半插入
最新推荐文章于 2022-01-12 13:12:50 发布