折半插入排序(binary insertion sort)
参考: http://www.cnblogs.com/foreverking/articles/2201805.html
http://blog.csdn.net/jiangtongcn/article/details/6402715
http://linjia880714.iteye.com/blog/986230
折半插入排序算法基本思想:
折半插入排序算法是对直接插入排序算法的一种改进。在直接插入排序算法中,向有序序列中插入一个元素,插入位置是把待插入元素关键字与有序序列中元素的关键字逐个比较得到的。
折半插入排序算法的基本思想是:
向有序序列中插入元素,那么插入位置可以不断地平分有序序列,并把待插入的元素的关键字与平分有序序列的关键字比较,以确定下一步要平分的子序列,直到找到合适的插入位置位置。
下面是Java的代码:
public class MyBinaryInsertionSort
{
public static void main(String[] args)
{
// 待排序的数组
int[] array = { 1, 0, 2, 5, 3, 4, 9, 8, 10, 6, 7};
binaryInsertSort(array);
// 显示排序后的结果。
System.out.print("排序后: ");
for(int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
}
// Binary Insertion Sort method
private static void binaryInsertSort(int[] array)
{
for(int i = 1; i < array.length; i++)
{
int temp = array[i];
int low = 0;
int high = i - 1;
while(low <= high)
{
int mid = (low + high) / 2;
if(temp < array[mid])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
for(int j = i; j >= low + 1; j--)
{
array[j] = array[j - 1];
}
array[low] = temp;
}
}
}