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;
}
}
}