class
InsertSort
{
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 ++ )
InsertSort.insertSort(array);
long timeEnd = System.currentTimeMillis();
System.out.println( " time: " + (timeEnd - timeStart));
for ( int num : array)
System.out.println(num);
}
static void insertSort( int [] array){
int temp;
int j,numToBeInserted;
for ( numToBeInserted = 1 ; numToBeInserted < array.length; numToBeInserted ++ ){
temp = array[numToBeInserted];
for ( j = numToBeInserted; j > 0 && temp < array[j - 1 ]; j -- ){
array[j] = array[j - 1 ];
}
array[j] = 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 ++ )
InsertSort.insertSort(array);
long timeEnd = System.currentTimeMillis();
System.out.println( " time: " + (timeEnd - timeStart));
for ( int num : array)
System.out.println(num);
}
static void insertSort( int [] array){
int temp;
int j,numToBeInserted;
for ( numToBeInserted = 1 ; numToBeInserted < array.length; numToBeInserted ++ ){
temp = array[numToBeInserted];
for ( j = numToBeInserted; j > 0 && temp < array[j - 1 ]; j -- ){
array[j] = array[j - 1 ];
}
array[j] = temp;
}
}
}