mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge pull request #156 from Deepak-j-p/master
Implemented sort for string
This commit is contained in:
commit
1d90764630
44
Sorting/CountingSortString.cpp
Normal file
44
Sorting/CountingSortString.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
// C++ Program for counting sort
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
string arr;
|
||||
cin>>arr;
|
||||
|
||||
countSort(arr);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user