package zju.zsq.binarysort;
import org.junit.Test;
public class BinarySort {
public void binaryInsertSort(int[] data) {
for (int i = 1; i < data.length; i++) {
if (data[i] < data[i - 1]) {
int tmp = data[i];
int low = 0;
int high = i - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (data[mid] < tmp) {
low = mid + 1;
} else {
high = mid - 1;
}
}
for (int j = i; j > low; j--) {
data[j] = data[j - 1];
}
data[low] = tmp;
for (int number : data) {
System.out.print(number + " ");
}
System.out.println();
}
}
}
@Test
public void testBubbleSort() {
int[] nums = { 1, 7, 3, 4, 5, 5, 9, 8, 11, 10 };
binaryInsertSort(nums);
System.out.println("排序之后:");
for (int num : nums) {
System.out.print(num + " ");
}
}
}
结果输出为:
1 3 7 4 5 5 9 8 11 10
1 3 4 7 5 5 9 8 11 10
1 3 4 5 7 5 9 8 11 10
1 3 4 5 5 7 9 8 11 10
1 3 4 5 5 7 8 9 11 10
1 3 4 5 5 7 8 9 10 11
排序之后:
1 3 4 5 5 7 8 9 10 11