You could do this in O(n^2).
Just go through the list n
times, setting the minimum element(while >= i
) to i
each time, where i
starts at 0 and increments to n-1
I suspect you're looking for something better than that, but I'm not sure how much better you can do in-place.
Example:
Input: 3 6 5 1
3 6 5 0*
1* 6 5 0
1 6 2* 0
1 3* 2 0
Note: this assumes elements are >= 0
and distinct