-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharCounting.cpp
More file actions
47 lines (36 loc) · 831 Bytes
/
CharCounting.cpp
File metadata and controls
47 lines (36 loc) · 831 Bytes
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
#include<iostream>
#include<cstring>
using namespace std;
void CountingSort(char arr[], int max, int size){
int count[max];
int output[size];
for(int i=0; i<max; i++){
count[i]=0;
}
for(int i=0; i<size; i++){
count[arr[i]]++;
}
for(int i=0; i<max; i++){
count[i]=count[i-1]+count[i];
}
for(int i=0; i<size; i++){
output[count[arr[i]]--] =arr[i];
}
for(int i=0; i<size; i++){
arr[i]=output[i];
}
}
int main(){
char arr[]="COUNTINGSORTEXAMPLE";
int size= strlen(arr);
int max=arr[0];
for(int i=0; i<size; i++){
if(arr[i]>max){
max=arr[i];
}
}
CountingSort(arr, max, size-1);
for(int i=0; i<size; i++){
cout<<arr[i];
}
}