Related Articles

# Patience Sorting

• Last Updated : 19 Jul, 2021

Patience sorting is a sorting algorithm based on card game Patience. In this sorting algorithm the rules of patience game is used to sort an list of elements based on their values.
Rules of Patience Game:

• Cards with lower value can be placed over the card.
• If there is no possible position for a card, then a new pile can be created.
• Goal is to form as much as few piles possible.

Below is the visualization of the game as follows:

As in the above visualization, It’s clear that cards are only placed when the value of them is less than the highest card of the pile. Otherwise, If there is no such pile then create a new one.
Patience Sorting: There are generally two steps in the patience sorting that is creation of the piles and merging the piles. Below is the illustration of the steps:

• Initialize a 2D array to store the piles.
• Traverse the given array and perform the following operations:
1. Iterate over all the piles and check the top of the stack of each pile is less than the current element or not. IF found to be true, then push the current element to the top of the stack.
2. Otherwise, create a new pile with the current element as the top of that stack.
• Merge the Piles:The idea is to perform a k-way merge of the p piles, each of which is internally sorted. Iterate over all the piles while count of elements in the pile is greater than or equal to 0 and find the minimum element from the top of each stack and push it into the sorted array.

Below is the visualization of the sorting steps:

Below is the implementation of the above approach:

## C++

 `// C++ program of the above approach` `#include ``using` `namespace` `std;` `// Function to merge piles in a sorted order``vector<``int``> merge_piles(vector >& v)``{` `    ``// Store minimum element from``    ``// the top of stack``    ``vector<``int``> ans;` `    ``// In every iteration find the smallest element``    ``// of top of pile and remove it from the piles``    ``// and store into the final array``    ``while` `(1) {` `        ``// Stores the smallest element``        ``// of the top of the piles``        ``int` `minu = INT_MAX;` `        ``// Stores index of the smallest element``        ``// of the top of the piles``        ``int` `index = -1;` `        ``// Calculate the smallest element``        ``// of the top of the every stack``        ``for` `(``int` `i = 0; i < v.size(); i++) {` `            ``// If minu is greater than``            ``// the top of the current stack``            ``if` `(minu > v[i][v[i].size() - 1]) {` `                ``// Update minu``                ``minu = v[i][v[i].size() - 1];` `                ``// Update index``                ``index = i;``            ``}``        ``}` `        ``// Insert the smallest element``        ``// of the top of the stack``        ``ans.push_back(minu);` `        ``// Remove the top element from``        ``// the current pile``        ``v[index].pop_back();` `        ``// If current pile is empty``        ``if` `(v[index].empty()) {` `            ``// Remove current pile``            ``// from all piles``            ``v.erase(v.begin() + index);``        ``}` `        ``// If all the piles are empty``        ``if` `(v.size() == 0)``            ``break``;``    ``}``    ``return` `ans;``}` `// Function to sort the given array``// using the patience sorting``vector<``int``> patienceSorting(vector<``int``> arr)``{` `    ``// Store all the created piles``    ``vector > piles;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < arr.size(); i++) {` `        ``// If no piles are created``        ``if` `(piles.empty()) {` `            ``// Initialize a new pile``            ``vector<``int``> temp;` `            ``// Insert current element``            ``// into the pile``            ``temp.push_back(arr[i]);` `            ``// Insert current pile into``            ``// all the piles``            ``piles.push_back(temp);``        ``}``        ``else` `{` `            ``// Check if top element of each pile``            ``// is less than or equal to``            ``// current element or not``            ``int` `flag = 1;` `            ``// Traverse all the piles``            ``for` `(``int` `j = 0; j < piles.size(); j++) {` `                ``// Check if the element to be``                ``// inserted is less than``                ``// current pile's top``                ``if` `(arr[i] < piles[j][piles[j].size() - 1]) {``                    ``piles[j].push_back(arr[i]);` `                    ``// Update flag``                    ``flag = 0;``                    ``break``;``                ``}``            ``}` `            ``// If flag is true``            ``if` `(flag) {` `                ``// Create a new pile``                ``vector<``int``> temp;` `                ``// Insert current element``                ``// into temp``                ``temp.push_back(arr[i]);` `                ``// Insert current pile``                ``// into all the piles``                ``piles.push_back(temp);``            ``}``        ``}``    ``}` `    ``// Store the sorted sequence``    ``// of the given array``    ``vector<``int``> ans;` `    ``// Sort the given array``    ``ans = merge_piles(piles);` `    ``// Traverse the array, ans[]``    ``for` `(``int` `i = 0; i < ans.size(); i++)``        ``cout << ans[i] << ``" "``;` `    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``vector<``int``> arr = { 6, 12, 2, 8, 3, 7 };` `    ``// Function Call``    ``patienceSorting(arr);``}`

## Javascript

 ``
Output:
`2 3 6 7 8 12`

Time Complexity: O(N2)
Auxiliary Space: O(N)

Note: The above approach can be optimized by merging of piles using priority_queue
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

</br>function playGif(){ </br> var gif = document.getElementById(‘gif’); </br> if (gif.src == “https://media.geeksforgeeks.org/wp-content/uploads/20200501171647/Patience.gif”){ </br> gif.src = “https://media.geeksforgeeks.org/wp-content/uploads/20200501175532/base2.jpg”; </br> }else{ </br> gif.src = “https://media.geeksforgeeks.org/wp-content/uploads/20200501171647/Patience.gif”; </br> } </br>} </br>function playSortingGif(){ </br> var gif1 = document.getElementById(‘sorting’); </br> var gif2 = document.getElementById(‘sorting_gif1’); </br> gif1.style.display = “none”; </br> gif2.style.display = “block”; </br>}</br>function pauseSortingGif(){ </br> var gif1 = document.getElementById(‘sorting’); </br> var gif2 = document.getElementById(‘sorting_gif1’); </br> gif1.style.display = “block”; </br> gif2.style.display = “none”; </br>} </br>

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up