Loading...
Loading...
Loading...
A card player sorts the cards in her hand with insertion sort: she takes the cards from the second one on, one at a time, and slides each card left past every bigger card until it fits.
Print the array after each card is inserted (n - 1 lines), then the total number of shifts. A shift is one bigger card moving one place to the right.
Shifts: s.Example
Input
5 7 3 9 1 5
Output
3 7 9 1 5 3 7 9 1 5 1 3 7 9 5 1 3 5 7 9 Shifts: 6
3 shifts 7 (1); 9 shifts nothing; 1 shifts 3 cards; 5 shifts 7 and 9 (2).
Try the problem first. Open one hint at a time.
Save the current card in key. Let j start just left of it.
While j ≥ 0 and a[j] > key, move a[j] one place right (one shift) and step j left.
Put key at j + 1 when the loop stops.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor