public class BinaryInsertSortTest {
public static void main(String[] args) {
int arr[]={3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
binaryInsertSort(arr);
for (int i : arr) {
System.out.print(i+" ");
}
}
private static void binaryInsertSort(int[] arr) {
for(int i=1;i< arr.length;i++){
int value=arr[i];
int left=0;
int right=i-1;
while(left<=right){
int middle=left+(right-left)/2;
if(arr[middle]>value){
left=middle+1;
}
else{
right=middle-1;
}
}
for (int j=i-1;j>=left;j–){
arr[j+1]=arr[j];
}
arr[left]=value;
} }
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)