A few problems in your for
loop:
Insertion has to use just 1 input per iteration (of the outside
for
), hence you have to assign the value to your temp variable before you get into yourwhile
for (i = 1; i < numbrid.length; i++) { k = numbrid[i];
Step on to the while loop with replacing the next number with the previous one till you reach your temp number (till your
numbrid[j] > k
)while ((j > -1) && (numbrid[j] > k)) { numbrid[j + 1] = numbrid[j]; j--; }
Assign your temp value to the next element in your array
numbridj + 1] = k;
Move on with your for loop
Hence, your for
loop becomes:
...
for (i = 1; i < numbrid.length; i++) {
k = numbrid[i];
j = i - 1;
while ((j > -1) && (numbrid[j] > k)) {
numbrid[j + 1] = numbrid[j];
j--;
}
numbrid[j + 1] = k;
}
...