Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a deck of cards [duplicate]

Given a deck of N cards. You have to sort them using following permissible operations:

  1. You can see top 2 cards.
  2. You can swap them.
  3. You can insert top at the bottom.

Any ideas?

like image 925
abhishekgarg Avatar asked Jun 27 '16 18:06

abhishekgarg


People also ask

How many ways can you sort a deck of cards?

It seems unbelievable, but there are somewhere in the range of 8x1067 ways to sort a deck of cards. That's an 8 followed by 67 zeros.

How do you sort playing cards?

The very first card in the sorted deck is the Ace of Clubs, the next ones are the King of Clubs, the Queen of Clubs, the Jack of Clubs, the 10 of Clubs ... down to the 2 of Clubs. The next card is Ace of Spades, followed by the King of Spades etc. The hearts and diamonds cards follow in the same order.


1 Answers

This seems to be a simple case of bubblesort, a very inefficient sorting algorithm that is described by smaller elements bubbling to the top (assuming that you are sorting in ascending order). The modified algorithm I'm about to present is very similar to the original bubblesort algorithm, so I am going to quickly explain the original first. Bubblesort (for ascending order) works as follows,

  1. The first element in a list is marked.
  2. If the element to the right of the marked element is less than the marked element, then the two elements are swapped.
  3. The marker moves to the right one spot regardless of the outcome of step two
  4. Steps two and three are repeated until the marked element becomes the last element in the list. Just for clarification, this means that when step 3 causes the last element to be marked, the iteration is over and a new iteration starts.

The four steps above are repeated until an iteration occurs where the marker goes through each element in the list without a single swap occurring. Here's an example from wikipedia, https://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example

So let's modify bubblesort so that a couple things change to fit the deck of cards scenario. Let's not think of the deck of cards as a deck, but more as a list. Yes, the first card in the deck will constantly be changing with each iteration of our modified bubblesort but can we make it so that cards are shifting while still maintaining the position of the first card in the deck? This question is to key to solving the problem. What I am saying is that moving the cards to the bottom of the deck will not change in the initial order of the cards, only swapping will. For example, consider this deck of cards where leftmost is the top and rightmost is the bottom:

NOTE: (*) signifies marked card

*5 3 1 2 6 

In the algorithm that will later be explained, moving 5 to the bottom of the deck will make the deck look like this

3 1 2 6 *5

Notice how 5 is now at the bottom of the deck, but the order is still preserved. The * symbol signifies the first card in the list/deck, so if read from left to right, starting from 5 and looping back to 3, the order is preserved.

Now to the algorithm, how do we use what I just said to make this modified version of bubblesort as similar to the original? Simple, use this marking mechanism to make it so that we're not really sorting a deck, but rather a list of numbers. Before starting the algorithm, mark the top card in the deck. Here are the rest of the steps for each iteration of bubblesort:

  1. Compare the top card with the card below it. If the top card is greater, then swap with the card below. If the marked card was swapped, then unmark the previously marked card and mark the new card at the top of the deck.
  2. Place the top card of the deck to the bottom.
  3. Steps 1 and 2 are repeated until the marked card resurfaces to being the second card in the deck (card below the top card)
  4. Put the top card to the bottom of the deck, making the marked card the top card in the deck.

These steps are repeated for each iteration of the algorithm until an iteration occurs where no swaps were made. Here is an example to showcase the modified bubblesort:

NOTE: (*) signifies marked card

Iteration One:

5 3 1 2 6 //Mark the first card in the deck before starting
*5 3 1 2 6 //Compare 5(top card) with 3(card below it)
3 *5 1 2 6 //5 > 3 so swap
*3 5 1 2 6 //Since the marked card (5) was swapped, 3 becomes the new marked 
           //card to preserve original order of the deck
5 1 2 6 *3 //Top card is placed at the bottom
1 5 2 6 *3 //5 > 1 so swap
5 2 6 *3 1 //Put 1 at the bottom
2 5 6 *3 1 //5 > 2 so swap
5 6 *3 1 2 //Put 2 at the bottom
5 6 *3 1 2 //5 < 6 so no swap
6 *3 1 2 5 //Put 5 at the bottom
*3 1 2 5 6 //Marked card is second to top card, so put 6 at the bottom
           //Marked card is now at the top, so new iteration begins

Before going into iteration two, I would like to point out that if you ran the original bubblesort, the resulting sequence from one iteration would be the same as the resulting sequence from one iteration of our modified algorithm.

Iteration Two:

*3 1 2 5 6 //3 > 1, so swap
*1 3 2 5 6 //Remark accordingly since the former marked card was swapped
3 2 5 6 *1 //Place 1 at the bottom
2 3 5 6 *1 //3 > 2, so swap
3 5 6 *1 2 //Place 2 at the bottom
3 5 6 *1 2 //3 < 5 so no swap
5 6 *1 2 3 //Place 3 at the bottom
5 6 *1 2 3 //5 < 6 so no swap
6 *1 2 3 5 //Place 5 at the bottom.
*1 2 3 5 6 //Since marked card is second to top card, place 6 at the bottom and end iteration

Iteration Three:

*1 2 3 5 6 //1 < 2 so no swap
2 3 5 6 *1 //Place 1 at the bottom
3 5 6 *1 2 //2 < 3 so no swap and place 2 at the bottom
5 6 *1 2 3 //3 < 5 so no swap and place 3 at the bottom
6 *1 2 3 5 //5 < 6 so no swap and place 5 at the bottom
*1 2 3 5 6 //Since marked card is second to top card, place 6 at the bottom and end iteration.

We now know to end the algorithm because an entire iteration has occurred without any swaps occurring, so the deck is now sorted. As for runtime, the worst case occurs, in terms of swaps, when the max number of iterations occurs which is n (size of the deck) times. And for each iteration, the worst case number of swaps occurs, which is also n times. So the big O is n*n or O(n^2).

like image 149
Chris Gong Avatar answered Sep 19 '22 21:09

Chris Gong