While condition is wrong:
while (i >= 0 && arrayIn[i] < arrayIn[j])
Should be:
while (i >= 0 && arrayIn[i] < temp)
Because arrayIn[j]
is nothing but arrayIn[i + 1]
in first iteration in while loop that can be over-written in arrayIn[i+1] = arrayIn[i];
in while.
In insertion sort you insert a value in sorted array, In outer loop you reads arrayIn[j]
in temp
. Now you are to insert temp
at sorted position, and for every arrayIn[i]
that is smaller than temp
a shift needed arrayIn[i+1] = arrayIn[i];
At first time you may write to arrayIn[j] = arrayIn[i];
if arrayIn[j] > arrayIn[i];
because j = i + 1
.
Edit, from @M Oehm:
Yours outer for loop run just for i = 1 to 3 and doesn't insert arrayIn[4]
to sorted position, change j < 5 - 1;
to j < 5
.