ArticleslgStudy

science

Patience sorting

Patience sorting is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Patience sorting rather than just read about it. In short: In computer science, patience sorting is a sorting algorithm inspired by, and named after, the card game patience. A variant of the algorithm efficiently computes the length of a longest increasing subsequence in a given array.

Key takeaways

  • Patience sorting belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Patience sorting to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Patience sorting from memory before moving on to harder problems.

Reference excerpt

In computer science, patience sorting is a sorting algorithm inspired by, and named after, the card game patience. A variant of the algorithm efficiently computes the length of a longest increasing subsequence in a given array.

Overview The algorithm's name derives from a simplified variant of the patience card game. The game begins with a shuffled deck of cards. The cards are dealt one by one into a sequence of piles on the table, according to the following rules.

Initially, there are no piles. The first card dealt forms a new pile consisting of the single card. Each subsequent card is placed on the leftmost existing pile whose top card has a value greater than or equal to the new card's value, or to the right of all of the existing piles, thus forming a new pile. When there are no more cards remaining to deal, the game ends. This card game is turned into a two-phase sorting algorithm, as follows. Given an array of n elements from some totally ordered domain, consider this array as a collection of cards and simulate the patience sorting game. When the game is over, recover the sorted sequence by repeatedly picking off the minimum visible card; in other words, perform a k-way merge of the p piles, each of which is internally sorted.

Pseudocode Below is an iterative implementation of Patience Sort, this implementation runs in O ( n 2 ) {\displaystyle O(n^{2})} .

function PatienceSorting(array arr) is n ← length(arr) piles ← empty list of lists

for i = 0 to n - 1 do if length(piles) == 0 then tmp ← new list tmp.append(arr[i]) piles.append(tmp) else placed ← false for j ← 0 to length(piles) - 1 do if arr[i] < last element of piles[j] then piles[j].append(arr[i]) placed ← true break if placed == false then tmp ← new list tmp.append(arr[i]) piles.append(tmp)

return MergePiles(piles)

function MergePiles(list of lists piles) is result ← empty list

while true do minValue ← ∞ minIndex ← -1

for i ← 0 to length(piles) - 1 do if length(piles[i]) > 0 and piles[i][length(piles[i]) - 1] < minValue then minValue ← piles[i][length(piles[i]) - 1] minIndex ← i if minIndex = -1 then break

result.append(minValue) piles[minIndex].remove(piles[minIndex][length(piles[minIndex]) - 1] if length(piles[minIndex]) == 0 then piles.remove(piles[minIndex])

return result

Analysis The first phase of patience sort, the card game simulation, can be implemented to take O(n log n) comparisons in the worst case for an n-element input array: there will be at most n piles, and by construction, the top cards of the piles form an increasing sequence from left to right, so the desired pile can be found by binary search. The second phase, the merging of piles, can be done in O ( n log ⁡ n ) {\displaystyle O(n\log n)} time as well using a priority queue. When the input data contain natural "runs", i.e., non-decreasing subarrays, then performance can be strictly better. In fact, when the input array is already sorted, all values form a single pile and both phases run in O(n) time. The average-case complexity is still O(n log n): any uniformly random sequence of values will produce an expected number of O ( n ) {\displaystyle O({\sqrt {n}})} piles, which take O ( n log ⁡ n ) = O ( n log ⁡ n ) {\displaystyle O(n\log {\sqrt {n}})=O(n\log n)} time to produce and merge. An evaluation of the practical performance of patience sort is given by Chandramouli and Goldstein, who show that a naive version is about ten to twenty times slower than a state-of-the-art quicksort on their benchmark problem. They attribute this to the relatively small amount of research put into patience sort, and develop several optimizations that bring its performance to within a factor of two of that of quicksort. If values of cards are in the range 1, . . . , n, there is an efficient implementation with O ( n log ⁡ n ) {\displaystyle O(n\log n)} worst-case running time for putting the cards into piles, relying on a Van Emde Boas tree.

Relations to other problems Patience sorting is closely related to a card game called Floyd's game. This game is very similar to the game sketched earlier:

The first card dealt forms a new pile consisting of the single card. Each subsequent card is placed on some existing pile whose top card has a value no less than the new card's value, or to the right of all of the existing piles, thus forming a new pile. When there are no more cards remaining to deal, the game ends. The object of the game is to finish with as few piles as possible. The difference with the patience sorting algorithm is that there is no requirement to place a new card on the leftmost pile where it is allowed. Patience sorting constitutes a greedy strategy for playing this game. Aldous and Diaconis suggest defining 9 or fewer piles as a winning outcome for n = 52, which happens with approximately 5% probability.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Patience sorting

Start with the simplest possible case. Write down what Patience sorting claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Patience sorting before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Patience sorting ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Patience sorting

In research
Patience sorting appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Patience sorting in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Patience sorting is common in secondary-school and first-year university syllabi. It links to neighbouring topics Comparison sorts, Patience games, so understanding it makes those chapters shorter.
In everyday life
Look for Patience sorting outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Patience sorting in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Patience sorting means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Patience sorting out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Patience sorting in simple terms?

In computer science, patience sorting is a sorting algorithm inspired by, and named after, the card game patience. A variant of the algorithm efficiently computes the length of a longest increasing subsequence in a given array.

Why does Patience sorting matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Patience sorting?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Patience sorting.

Tags

  • Comparison sorts
  • Patience games

Keep exploring