Skip to content

Commit

Permalink
Count sort algorithm
Browse files Browse the repository at this point in the history
Count sort code for sorting alphabets in ascending order in cpp.
  • Loading branch information
GarvitV957 authored Oct 7, 2021
1 parent e59488a commit b2e124f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Count_sort_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
using namespace std;

void cSort(char s[]) {
string output;

int count[256], i;
for (int i = 0; i < 256; i++){
count[i] = 0;
}
for (i = 0; s[i]; ++i){
++count[s[i]];
}
for (i = 1; i < 256; ++i){
count[i] += count[i - 1];
}
for (i = 0; s[i]; ++i) {
output[count[s[i]] - 1] = s[i];
--count[s[i]];
}

for (i = 0; s[i]; ++i){
s[i] = output[i];
}
cout << "Sorted string is: " << s;
}

int main() {
int n;
cin>>n;
char s[n];
cin>>s;

cSort(s);
return 0;
}

0 comments on commit b2e124f

Please sign in to comment.