-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_CountingSort.cpp
68 lines (56 loc) · 1.27 KB
/
GFG_CountingSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
https://practice.geeksforgeeks.org/problems/counting-sort/1
Counting Sort
*/
// { Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
#define RANGE 255
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
//Function to arrange all letters of a string in lexicographical
//order using Counting Sort.
string countSort(string arr){
// code here
int k =26;
int C[k] = {0};
int n = arr.length(), ind =-1;
string ans(n,'x');
for(int i=0; i<n; i++)
{
ind = arr[i]-97;
C[ind] = C[ind]+1; // number of elements equal to i;
}
for(int i=1; i<k; i++)
{
C[i] = C[i] + C[i-1]; // number of elements less than or equal to i
}
for(int j=n-1; j>=0; j--)
{
ind = arr[j]-97;
ans[C[ind]-1] = arr[j];
C[ind] = C[ind]-1;
}
return ans;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string arr;
cin>>arr;
Solution obj;
cout<<obj.countSort(arr)<<endl;
}
return 0;
}
// } Driver Code Ends