From 5f974de07be64f469d4361315cb7d3cde0085fca Mon Sep 17 00:00:00 2001 From: Koustubh Mane <114218468+Koustubh-Mane1@users.noreply.github.com> Date: Tue, 4 Oct 2022 23:40:16 +0530 Subject: [PATCH 1/2] Added Patience Sort in Sorting Algorithms --- C codes/Sorting/Patience_Sort.cpp | 84 +++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 C codes/Sorting/Patience_Sort.cpp diff --git a/C codes/Sorting/Patience_Sort.cpp b/C codes/Sorting/Patience_Sort.cpp new file mode 100644 index 0000000..eaad4de --- /dev/null +++ b/C codes/Sorting/Patience_Sort.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + + +vector merge_piles(vector >& v) +{ + + vector ans; + + while (1) { + + int minu = INT_MAX; + + int index = -1; + + for (int i = 0; i < v.size(); i++) { + if (minu > v[i][v[i].size() - 1]) { + + minu = v[i][v[i].size() - 1]; + + index = i; + } + } + + + ans.push_back(minu); + + + v[index].pop_back(); + if (v[index].empty()) { + + v.erase(v.begin() + index); + } + if (v.size() == 0) + break; + } + return ans; +} +vector patienceSorting(vector arr) +{ + vector > piles; + for (int i = 0; i < arr.size(); i++) { + if (piles.empty()) { + vector temp; + temp.push_back(arr[i]); + piles.push_back(temp); + } + else { + int flag = 1; + + for (int j = 0; j < piles.size(); j++) { + if (arr[i] < piles[j][piles[j].size() - 1]) { + piles[j].push_back(arr[i]); + flag = 0; + break; + } + } + + if (flag) { + vector temp; + + temp.push_back(arr[i]); + + piles.push_back(temp); + } + } + } + + vector ans; + + ans = merge_piles(piles); + + for (int i = 0; i < ans.size(); i++) + cout << ans[i] << " "; + + return ans; +} + + +int main() +{ + vector arr = { 6, 12, 2, 8, 3, 7 }; + patienceSorting(arr); +} From 31b6c151579be51401bff7407a7c185c54daeb30 Mon Sep 17 00:00:00 2001 From: Koustubh Mane <114218468+Koustubh-Mane1@users.noreply.github.com> Date: Tue, 4 Oct 2022 23:41:35 +0530 Subject: [PATCH 2/2] Added Patience Sort in Sorting Algorithms --- C codes/Sorting/Patience_Sort.cpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/C codes/Sorting/Patience_Sort.cpp b/C codes/Sorting/Patience_Sort.cpp index eaad4de..d449daf 100644 --- a/C codes/Sorting/Patience_Sort.cpp +++ b/C codes/Sorting/Patience_Sort.cpp @@ -4,31 +4,19 @@ using namespace std; vector merge_piles(vector >& v) { - vector ans; - while (1) { - int minu = INT_MAX; - int index = -1; - for (int i = 0; i < v.size(); i++) { if (minu > v[i][v[i].size() - 1]) { - minu = v[i][v[i].size() - 1]; - index = i; } } - - ans.push_back(minu); - - v[index].pop_back(); if (v[index].empty()) { - v.erase(v.begin() + index); } if (v.size() == 0) @@ -47,7 +35,6 @@ vector patienceSorting(vector arr) } else { int flag = 1; - for (int j = 0; j < piles.size(); j++) { if (arr[i] < piles[j][piles[j].size() - 1]) { piles[j].push_back(arr[i]); @@ -58,21 +45,16 @@ vector patienceSorting(vector arr) if (flag) { vector temp; - temp.push_back(arr[i]); - piles.push_back(temp); } } } vector ans; - ans = merge_piles(piles); - for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; - return ans; }