Short answer: Patience sorting deals each item onto the leftmost pile whose top is greater than or equal to the item. The number of piles equals the length of a longest increasing subsequence. With binary search over the pile tops, building the piles is O(n log n). A priority queue of those tops then emits a full sort in the same bound. The piles are the algorithm. The subsequence and the sort are two readings of them.
What the problem actually asks
An increasing subsequence uses items in their original relative order, each strictly larger than the one before, or nondecreasing if equals are allowed. The longest one can be found by dynamic programming in Θ(n²). Patience sorting is the faster method, and it has a direct card-game picture: a new card starts a new pile only when it is smaller than every pile top it could extend.
The same piles, read through a priority queue, sort the multiset. That second use is real and usually less important than the subsequence length, which falls out of the pile count with no extra search.
What the algorithm guarantees
If a new item goes on the leftmost pile whose top is ≥ the item, the pile tops stay sorted and the number of piles equals the length of a longest nondecreasing subsequence. Strict inequality in the placement rule switches the result to a longest strictly increasing subsequence. Binary search finds the pile in O(log n), so the construction is O(n log n) time and O(n) extra memory in the worst case, when every item starts a pile.
Recovering one actual subsequence, rather than only its length, needs a predecessor pointer from each item to the top it covered. Following those pointers from the last pile yields the subsequence in reverse. Sorting is a different traversal: repeatedly extract the smallest pile top and append it. With a heap that is also O(n log n), and it is a full sort of the multiset, not a subsequence.
The working steps
- Start with no piles.
- For each item in order, binary-search the pile tops for the leftmost top that is ≥ the item. For a strictly increasing subsequence, search for a top that is > the item.
- Place the item on that pile. If no such pile exists, append a new pile.
- The number of piles is the subsequence length. Store a predecessor if one witness subsequence is required, and walk it backward from any item that extended the last pile.
- To sort instead, put the pile tops in a heap and repeatedly append the smallest top, exposing the card beneath it.
The O(n log n) bound is for a comparison-based subsequence in a sequence, not for a substring and not for a common subsequence of two strings. Longest common subsequence of two strings is a different grid. The pile count is the length. Without predecessor links the items themselves are not recovered.
Approaches that look simpler and fail
| Approach | Why it fails when the pile rule is loosened |
|---|---|
| Place the item on any pile whose top is large enough | The pile count can then exceed the subsequence length. The leftmost legal pile is the rule that keeps the tops sorted and the count exact. |
| Use ≥ in the search and claim a strictly increasing subsequence | Equals share a pile under ≥ and may both appear in a recovered chain. Strict increase needs a strict comparison. |
| Dynamic programming that stores the best length at every index | That recurrence is correct and Θ(n²). The piles are what bring the same answer down to O(n log n). |
| Read the last pile as the subsequence | Each pile holds many items that are not one increasing chain. The chain is the predecessor path, one item from each preceding stage. |
A quadratic longest-common-subsequence of two strings belongs to the LCS grid. A general sort that is stable and adaptive belongs to timsort. The heap extraction from piles does sort, and it is not the reason this structure is usually kept.
A working example
The Bash script and the Python script patience-sort into nondecreasing piles. The number of piles is the longest nondecreasing subsequence. On 3, 1, 2, 1, 4 both print 3.
a=(3 1 2 1 4)
pc=0
for x in "${a[@]}"; do
lo=0
hi=$pc
while (( lo < hi )); do
mid=$(( (lo+hi)/2 ))
if (( piles[mid] < x )); then lo=$((mid+1))
else hi=$mid; fi
done
if (( lo == pc )); then piles[pc]=$x; pc=$((pc+1))
else piles[lo]=$x; fi
done
echo "$pc"
import bisect
a = [3, 1, 2, 1, 4]
piles = []
for x in a:
i = bisect.bisect_left(piles, x)
if i == len(piles):
piles.append(x)
else:
piles[i] = x
assert len(piles) == 3
print(len(piles))
Where it is used
Patience sorting computes a longest increasing subsequence, the measure of how much of a sequence is already in order. Diff and permutation statistics use that length. A full sort answers a different question and destroys the subsequence.
When you can drop this algorithm
Drop the piles when only a full sort is required and a library sort is available. Keep them when the longest increasing or nondecreasing subsequence length, or one witness subsequence, is the result. The construction is then O(n log n) and the pile count is the length.
Why this stays published
We publish this so a search for patience sorting returns the leftmost-pile rule and the fact that the pile count is the subsequence length. Patience sorting deals each item onto the leftmost legal pile, and the number of piles is the length of a longest increasing subsequence.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
