HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, patience sorting is a
sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending ...
inspired by, and named after, the card game
patience or forbearance, is the ability to endure difficult or undesired long-term circumstances. Patience involves perseverance or tolerance in the face of delay, provocation, or stress without responding negatively, such as reacting with disrespect ...
. 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 elements from some
totally ordered In mathematics, a total order or linear order is a partial order in which any two elements are comparable. That is, a total order is a binary relation \leq on some set X, which satisfies the following for all a, b and c in X: # a \leq a ( r ...
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 -way merge of the piles, each of which is internally sorted.


Pseudocode

Below is an iterative implementation of Patience Sort, this implementation runs in 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 piles.append(tmp) else placed ← false for j ← 0 to length(piles) - 1 do if arr < last element of piles then piles append(arr placed ← true break if placed

false then tmp ← new list tmp.append(arr 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 > 0 and piles length(piles - 1] < minValue then minValue ← piles length(piles - 1] minIndex ← i if minIndex = -1 then break result.append(minValue) piles inIndexremove(piles inIndexlength(piles inIndex - 1] if length(piles inIndex

0 then piles.remove(piles inIndex return result


Analysis

The first phase of patience sort, the card game simulation, can be implemented to take comparisons in the worst case for an -element input array: there will be at most 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 In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...
. The second phase, the merging of piles, can be done in O(n\log n) time as well using a
priority queue In computer science, a priority queue is an abstract data type similar to a regular queue (abstract data type), queue or stack (abstract data type), stack abstract data type. In a priority queue, each element has an associated ''priority'', which ...
. 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 time. The average-case complexity is still : any uniformly random sequence of values will produce an expected number of O(\sqrt) piles, which take O(n\log\sqrt) = 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 Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961. It is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
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 , there is an efficient implementation with 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 , which happens with approximately 5% probability.


Algorithm for finding a longest increasing subsequence

First, execute the sorting algorithm as described above. The number of piles is the length of a longest subsequence. Whenever a card is placed on top of a pile, put a
back-pointer In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer ''re ...
to the top card in the previous pile (that, by assumption, has a lower value than the new card has). In the end, follow the back-pointers from the top card in the last pile to recover a decreasing subsequence of the longest length; its reverse is an answer to the longest increasing subsequence algorithm. S. Bespamyatnikh and M. Segal give a description of an efficient implementation of the algorithm, incurring no additional
asymptotic In analytic geometry, an asymptote () of a curve is a line such that the distance between the curve and the line approaches zero as one or both of the ''x'' or ''y'' coordinates Limit of a function#Limits at infinity, tends to infinity. In pro ...
cost over the sorting one (as the back-pointers storage, creation and traversal require linear time and space). They further show how to report ''all'' the longest increasing subsequences from the same resulting
data structure In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
s.


History

Patience sorting was named by C. L. Mallows, who attributed its invention to A.S.C. Ross in the early 1960s. According to Aldous and Diaconis, patience sorting was first recognized as an algorithm to compute the longest increasing subsequence length by Hammersley. A.S.C. Ross and independently Robert W. Floyd recognized it as a sorting algorithm. Initial analysis was done by Mallows. Floyd's game was developed by Floyd in correspondence with
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist and mathematician. He is a professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of comp ...
.


Use

The patience sorting algorithm can be applied to
process control Industrial process control (IPC) or simply process control is a system used in modern manufacturing which uses the principles of control theory and physical industrial control systems to monitor, control and optimize continuous Industrial processe ...
. Within a series of measurements, the existence of a long increasing subsequence can be used as a trend marker. A 2002 article in SQL Server magazine includes a SQL implementation, in this context, of the patience sorting algorithm for the length of the longest increasing subsequence.


References

{{sorting Comparison sorts Patience games