Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedang1510 authored Dec 26, 2023
1 parent 28ded07 commit 82d233f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Add Code Here/counting_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
using namespace std;

class Solution{

public:
string countingSort(string str){
int maximum = (str[0] - 'a');
for (int i=0; i<str.length(); i++)
{
maximum = max(maximum, (str[i] - 'a'));
}

int count[maximum+1] = {0};
for (int i=0; i<str.length(); i++)
count[str[i]-'a']++; // correct

for (int i=1; i<=maximum; i++)
count[i] += count[i-1];

string ans[str.length()];
for (int i=str.length()-1; i>=0; i--)
ans[--count[(str[i]-'a')]] = str[i];

string fans = "";
for (int i=0; i<str.length(); i++)
fans += ans[i];
return fans;
}
};
int main()
{
Solution sol = Solution();
cout << "Enter string : ";
string str;
getline(cin, str);
string ans = sol.countingSort(str);
cout << ans;
return 0;
}

0 comments on commit 82d233f

Please sign in to comment.