-
Notifications
You must be signed in to change notification settings - Fork 0
/
countSort.cpp
58 lines (46 loc) · 1009 Bytes
/
countSort.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
#include<iostream>
#include<vector>
using namespace std;
void CountSort(vector<int> &v){
int n=v.size();
//find the max element
int max_ele=INT_MIN;
for(int i=0;i<n;i++){
// if(max_ele<v[i]){
// max_ele=v[i];
// }
max_ele=max(v[i],max_ele);
}
//create the freq array
vector<int>freq(max_ele+1,0);
for(int i=0;i<n;i++){
freq[v[i]]++;
}
//calculate cumulative freq
for(int i=1;i<=max_ele;i++){
freq[i]+=freq[i-1];
}
//create the ans array
vector<int>ans(n);
//calculate sorted array
for(int i=n-1;i>=0;i--){
ans[--freq[v[i]]]=v[i];
}
//copy back the ans to origional array
for(int i=0;i<n;i++){
v[i]=ans[i];
}
}
int main(){
int n;
cin>>n;
vector<int>v(n);
for(int i=0;i<v.size();i++){
cin>>v[i];
}
CountSort(v);
cout<<"sorted Array:"<<endl;
for(int i=0;i<n;i++){
cout<<v[i]<<" ";
}
}