Actually you are confusing the while
loop with do-while
. The first do
here is not a part of while
. So, it's not a do-while
loop.
It's just telling to do the shown assignment inside the for
loop, before the while
loop starts.
So, the equivalent code would be like:
for (int j = 2; j < n; ++j) {
int key = A[j];
int i = j - 1;
while (i > 0 && A[i] > key) {
A[i+1] = A[i];
i = i - 1; // can be replaced with '--i'
}
A[i + 1] = key
}