From 15315d60470350a048371ab4fa545143a3f64243 Mon Sep 17 00:00:00 2001 From: Deepak-j-p Date: Wed, 31 Oct 2018 09:48:26 +0530 Subject: [PATCH 1/2] Implemented sort for string Implemented a counting sort for string in cpp --- Sorting/CountingSortString.cpp | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Sorting/CountingSortString.cpp diff --git a/Sorting/CountingSortString.cpp b/Sorting/CountingSortString.cpp new file mode 100644 index 000000000..3ebe36dcf --- /dev/null +++ b/Sorting/CountingSortString.cpp @@ -0,0 +1,44 @@ +// C++ Program for counting sort +#include + +using namespace std; + +void countSort(string arr) +{ + + string output; + + int count[256], i; + for(int i=0;i<256;i++) + count[i]=0; + + + for(i = 0; arr[i]; ++i) + ++count[arr[i]]; + + + for (i = 1; i <= 256; ++i) + count[i] += count[i-1]; + + for (i = 0; arr[i]; ++i) + { + output[count[arr[i]]-1] = arr[i]; + --count[arr[i]]; + } + + for (i = 0; arr[i]; ++i) + arr[i] = output[i]; + + cout<<"Sorted character array is "<>arr; + + countSort(arr); + + return 0; +} From 46970cd5fc517ef58ecae154a5797f3becc197e2 Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Sun, 10 Feb 2019 12:32:07 +0530 Subject: [PATCH 2/2] Update CountingSortString.cpp --- Sorting/CountingSortString.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorting/CountingSortString.cpp b/Sorting/CountingSortString.cpp index 3ebe36dcf..2dbf499e7 100644 --- a/Sorting/CountingSortString.cpp +++ b/Sorting/CountingSortString.cpp @@ -1,5 +1,5 @@ // C++ Program for counting sort -#include +#include using namespace std;