标签:二分插入排序
博客地址:http://blog.csdn.net/mkrcpp/article/details/39153951
import java.util.Arrays;
public class Main
{
// 循环测试次数
public static int LOOP_COUNT = 100;
public static int ARRAY_SIZE = 10000;
public static void main(String[] args)
{
int[] mArray = Common.getArray(ARRAY_SIZE);
int allTime = 0;
for (int i = 0; i < LOOP_COUNT; i++)
{
// 拷贝数组
int[] tmpArray = Arrays.copyOf(mArray, ARRAY_SIZE);
long tmpTime = System.currentTimeMillis();
execute(tmpArray);
allTime += System.currentTimeMillis() - tmpTime;
}
System.err.println(LOOP_COUNT + "次排序的平均耗时:" + allTime / (float) LOOP_COUNT);
}
/***
* @title 二分插入排序
* @description
* @author michael.mao
* @date 2014年9月5日 下午2:42:29
* @version V1.0
*/
public static void execute(int[] array)
{
for (int i = 1; i < array.length; i++)
{
int tmpV = array[i];
int mid = 0, left = 0, right = i - 1;
while (right >= left)
{
mid = (left + right) / 2;// 中
if ( tmpV > array[mid] )
left = mid + 1;
else
right = mid - 1;
}
// 目标(mid == left ==right)右侧(不包括目标本身)至结尾(i)右移
for (int j = i - 1; j >= mid + 1; j--)
array[j + 1] = array[j];
// 目标赋值
array[mid] = tmpV;
}
}
}
数组大小为(10000)的100次二分插入排序的平均耗时为:10.15 ms
标签:二分插入排序
原文地址:http://blog.csdn.net/mkrcpp/article/details/39153951