Skip to content

Commit 1d90764

Browse files
authored
Merge pull request #156 from Deepak-j-p/master
Implemented sort for string
2 parents f3ba908 + 46970cd commit 1d90764

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Sorting/CountingSortString.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// C++ Program for counting sort
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
void countSort(string arr)
7+
{
8+
9+
string output;
10+
11+
int count[256], i;
12+
for(int i=0;i<256;i++)
13+
count[i]=0;
14+
15+
16+
for(i = 0; arr[i]; ++i)
17+
++count[arr[i]];
18+
19+
20+
for (i = 1; i <= 256; ++i)
21+
count[i] += count[i-1];
22+
23+
for (i = 0; arr[i]; ++i)
24+
{
25+
output[count[arr[i]]-1] = arr[i];
26+
--count[arr[i]];
27+
}
28+
29+
for (i = 0; arr[i]; ++i)
30+
arr[i] = output[i];
31+
32+
cout<<"Sorted character array is "<<arr;
33+
}
34+
35+
36+
int main()
37+
{
38+
string arr;
39+
cin>>arr;
40+
41+
countSort(arr);
42+
43+
return 0;
44+
}

0 commit comments

Comments
 (0)