TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
counting_sort_string.cpp
1// C++ Program for counting sort
2#include <iostream>
3
4using namespace std;
5
6void countSort(string arr) {
7 string output;
8
9 int count[256], i;
10 for (int i = 0; i < 256; i++) count[i] = 0;
11
12 for (i = 0; arr[i]; ++i) ++count[arr[i]];
13
14 for (i = 1; i < 256; ++i) count[i] += count[i - 1];
15
16 for (i = 0; arr[i]; ++i) {
17 output[count[arr[i]] - 1] = arr[i];
18 --count[arr[i]];
19 }
20
21 for (i = 0; arr[i]; ++i) arr[i] = output[i];
22
23 cout << "Sorted character array is " << arr;
24}
25
26int main() {
27 string arr;
28 cin >> arr;
29
30 countSort(arr);
31
32 return 0;
33}
int main()
Main function.