Initially, the order of cards in the deck (if you list cards from top to bottom) is {100, 50, 60, 30}. Pupkin will perform the following actions:
- Get a card with the number 100 and put it down the deck, as the number written on it is not the minimum of the remaining ones. The order of cards in the deck after that will be {50, 60, 30, 100}.
- Get a card with the number 50 and put it down the deck, since the number written on it is not the minimum of the remaining ones. The order of cards in the deck after that will be {60, 30, 100, 50}.
- Get a card with the number 60 and put it down the deck, as the number written on it is not the minimum of the remaining ones. The order of cards in the deck after that will be {30, 100, 50, 60}.
- Get a card with the number 30 and remove it, since the number written on it is the minimum of the remaining ones. The order of cards in the deck after that will be {100, 50, 60}.
- Get a card with the number 100 and put it down the deck, as the number written on it is not the minimum of the remaining ones. The order of cards in the deck after that will be {50, 60, 100}.
- Pull out a card with the number 50 and remove it, since the number written on it is the minimum of the remaining ones. The order of cards in the deck after that will be {60, 100}.
- Get a card with the number 60 and remove it, since the number written on it is the minimum of the remaining ones. The order of cards in the deck after that becomes {100}.
- Get a card with the number 100 and remove it, since the number written on it is the minimum of the remaining ones. After that, there will be no cards left in the deck.
Thus, Pupkin will look at the top card of the deck 8 times to sort the cards in it. Write code that implements all of this in Java SE. Use only one-dimensional array, for example: int array [] = {100, 50, 60, 30}; here is my implementation, but not true:
int array[] = new int[5]; array[0] = 100; array[1] = 50; array[2] = 60; array[3] = 30; array[4] = 101; for(int i = 0; i < array.length; i++){ int min = array[i]; int imin = i; for (int j = i+1; j < array.length; j++) { if(min < array[j]){ min = array[j]; imin = j; } } if (i != imin) { int temp = array[i]; array[i] = array[imin]; array[imin] = temp; } System.out.print(" "+array[i]); }