Think about how to implement this algorithm. Which data structures and algorithms would you use for each part?
The collection of piles
Each individual pile
Finding the right pile during phase one
Merging the piles during phase two
Here is a game which begins with a shuffled deck of cards, which are then dealt one-by-one into a sequence of piles on the table according to the following rules:
1. The first card dealt forms the first pile.
2. Every subsequent card dealt is placed on the leftmost existing pile whose top card is greater than or equal to the new card's value, or forms a new pile to the right of all existing piles.
3. The game ends when there are no more cards to deal.
Consider a two-phase sorting algorithm based upon this game as follows:
1. Given an array of N elements, consider this array to be the shuffled deck of cards and play the above game.
2. When the game ends, form the sorted sequence by repeatedly picking up the minimum visible card until all piles are empty.