BinaryInsertSort
class BinaryInsertSort
{
public static void main(String[] args)
{
int[] array = new int[]{ 3, 5,2,8,4,9,-1,7 };
for(int num : array)
System.out.println(num);
long timeStart = System.currentTimeMillis();
for(int i=0; i<10000000; i++)
BinaryInsertSort.binaryInsertSort(array);
long timeEnd = System.currentTimeMillis();
System.out.println("time: "+(timeEnd-timeStart));
for(int num : array)
System.out.println(num);
}
static void binaryInsertSort(int[] array){
int temp;
int j,numToBeInserted;
int left,right,middle;
for(numToBeInserted=1; numToBeInserted<array.length; numToBeInserted++){
left = 0;
right = numToBeInserted-1;
temp = array[numToBeInserted];
while(left<=right){
middle = (left+right)/2;
if(temp<array[middle])
right = middle-1;
else
left = middle+1;
}
for(j=numToBeInserted; j>left; j--)
array[j] = array[j-1];
array[left] = temp;
}
}
}
{
public static void main(String[] args)
{
int[] array = new int[]{ 3, 5,2,8,4,9,-1,7 };
for(int num : array)
System.out.println(num);
long timeStart = System.currentTimeMillis();
for(int i=0; i<10000000; i++)
BinaryInsertSort.binaryInsertSort(array);
long timeEnd = System.currentTimeMillis();
System.out.println("time: "+(timeEnd-timeStart));
for(int num : array)
System.out.println(num);
}
static void binaryInsertSort(int[] array){
int temp;
int j,numToBeInserted;
int left,right,middle;
for(numToBeInserted=1; numToBeInserted<array.length; numToBeInserted++){
left = 0;
right = numToBeInserted-1;
temp = array[numToBeInserted];
while(left<=right){
middle = (left+right)/2;
if(temp<array[middle])
right = middle-1;
else
left = middle+1;
}
for(j=numToBeInserted; j>left; j--)
array[j] = array[j-1];
array[left] = temp;
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.