2019-08-21 10:10:08 +08:00
|
|
|
// C++ Program for counting sort
|
2019-02-10 15:02:07 +08:00
|
|
|
#include <iostream>
|
2019-08-21 10:10:08 +08:00
|
|
|
|
2018-10-31 12:18:26 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
void countSort(string arr)
|
|
|
|
{
|
2018-10-31 12:18:26 +08:00
|
|
|
|
|
|
|
string output;
|
2019-08-21 10:10:08 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2018-10-31 12:18:26 +08:00
|
|
|
string arr;
|
2019-08-21 10:10:08 +08:00
|
|
|
cin >> arr;
|
|
|
|
|
|
|
|
countSort(arr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|