Patience sorting
In computer science, patience sorting is a sorting algorithm inspired by, and named after, the card game patience.[src] A variant of the algorithm efficiently computes the length of a longest increasing subsequence in a given array.[src]
In computer science, patience sorting is a sorting algorithm inspired by, and named after, the card game patience.[src] A variant of the algorithm efficiently computes the length of a longest increasing subsequence in a given array.[src] ==Overview== The algorithm's name derives from a simplified variant of the patience card game.[src] The game begins with a shuffled deck of cards.[src] The cards are dealt one by one into a sequence of piles on the table, according to the following rules.[src] Initially, there are no piles.[src] The first card dealt forms a new pile consisting of the single card.[src] 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.[src] When there are no more cards remaining to deal, the game ends.[src] This card game is turned into a two-phase sorting algorithm, as follows.[src] Given an array of elements from some totally ordered domain, consider this array as a collection of cards and simulate the patience sorting game.[src] 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.[src] 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] 0 and piles[i][length(piles[i]) - 1] O(\sqrt{n}) piles, which take O(n\log\sqrt{n}) = O(n\log n) time to produce and merge.[src] 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.[src] 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.[src] 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.[src] ==Relations to other problems== Patience sorting is closely related to a card game called Floyd's game.[src] This game is very similar to the game sketched earlier: The first card dealt forms a new pile consisting of the single card.[src] 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.[src] When there are no more cards remaining to deal, the game ends.[src] The object of the game is to finish with as few piles as possible.[src] 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.[src] Patience sorting constitutes a greedy strategy for playing this game.[src] Aldous and Diaconis suggest defining 9 or fewer piles as a winning outcome for , which happens with approximately 5% probability.[src] ===Algorithm for finding a longest increasing subsequence=== First, execute the sorting algorithm as described above.[src] The number of piles is the length of a longest increasing subsequence.[src] Whenever a card is placed on top of a pile, put a back-pointer to the top card in the previous pile (that, by assumption, has a lower value than the new card has).[src] 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.[src] Bespamyatnikh and Segal describe an efficient implementation of this algorithm that incurs no additional asymptotic cost over the sorting one (as the back-pointers storage, creation and traversal require linear time and space).[src] They further show how to report all longest increasing subsequences from the same resulting data structures.[src] ==History== Patience sorting was named by Colin Lingwood Mallows, who attributed its invention to A.S.C.[src] Ross in the early 1960s.[src] According to Aldous and Diaconis, patience sorting was first recognized as an algorithm to compute the longest increasing subsequence length by Hammersley.[src] A.S.C.[src] Ross and independently Robert W.[src] Floyd recognized it as a sorting algorithm.[src] Initial analysis was done by Mallows.[src] Floyd's game was developed by Floyd in correspondence with Donald Knuth.[src] ==Use== The patience sorting algorithm can be applied to process control.[src] Within a series of measurements, the existence of a long increasing subsequence can be used as a trend marker.[src] 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.[src] ==References==[src]
44 sentences · 44 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Patience sorting." encyclopegia.org, 2026, https://encyclopegia.org/wiki/patience-sorting. Accessed 26 November 2026.
Chicago
"Patience sorting." encyclopegia.org. Accessed November 26, 2026. https://encyclopegia.org/wiki/patience-sorting.
APA
encyclopegia.org. (2026). Patience sorting. Retrieved November 26, 2026, from https://encyclopegia.org/wiki/patience-sorting
Accessed November 26, 2026 · CC BY-SA 4.0 · citation page · JSON