I'm studying this book since yesterday and after I've understood and applied the first algorithm, I tried to go on my own and look in a different way. Here's, in Java, the shown algorithm :
public static int[] sort(int[] array)
{
for(int i = 1; i < array.length; i++){
int value = array[i];
int j = i - 1;
while(j >= 0 && array[j] > value){
array[j + 1] = array[j];
j--;
}
array[j+1] = value;
}
return array;
}
And here is mine :
public static int[] sortb(int[] array)
{
for(int i = 0; i < array.length; i++){
int value = array[i];
int j = i;
while(j < array.length && value > array[j]){
array[j] = array[j + 1];
j++;
}
array[j] = value;
}
return array;
}
For 1 million of function call for each, I got 32 ms for the first and 25 ms for the second. I'm still beginning with algorithms, so I have no idea of the meaning.