折半查找插入排序

import java.awt.print.Printable;


public class InsertSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {3,1,5,7,2,4,9,6};
print(a);
new InsertSort().binaryInsertSort(a);
}


private void binaryInsertSort(int[] a) {
// TODO Auto-generated method stub
int tempDate;
int high;
int low;
int middle;
for (int i = 1; i < a.length; i++) {
  tempDate=a[i];
  low=0;
  high=i-1;
  while (low<=high) {
  middle=(low+high)/2;
if (a[middle] low=middle+1;
}else {
high=middle-1;
}

}
  for (int j = i; j >low; j--) {
a[j]=a[j-1];
}
  a[low]=tempDate;
  print(a);
}
}


private static void print(int[] a) {
// TODO Auto-generated method stub
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}

}

运行结果:

3 1 5 7 2 4 9 6 
1 3 5 7 2 4 9 6 
1 3 5 7 2 4 9 6 
1 3 5 7 2 4 9 6 
1 2 3 5 7 4 9 6 
1 2 3 4 5 7 9 6 
1 2 3 4 5 7 9 6 
1 2 3 4 5 6 7 9 

时间复杂度O(n*n),空间复杂度O(1)

你可能感兴趣的:(数据结构排序)