-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountsort.cpp
42 lines (37 loc) · 866 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
#include <bits/stdc++.h>
using namespace std;
void countsort(vector<int> &vect){
int n=vect.size();
int maxele;
for(int i=0;i<n;i++){
maxele=max(maxele,vect[i]);
}
//now we will create an array of frequency till maxele;
vector<int>freq(maxele + 1 , 0);
for(int i=0;i<n;i++){
freq[vect[i]]++;
}
//now we will calculate cumulative frquency function
for(int i=1;i<=maxele;i++){
freq[i]=freq[i]+freq[i-1];
}
vector<int>temp(n);
for(int i=n-1;i>=0;i--){
temp[--freq[vect[i]]]=vect[i];
}
for(int i=0;i<n;i++){
vect[i]=temp[i];
}
}
int main(){
int n;
cin>>n;
vector <int> givenarray(n);
for(int i=0;i<n;i++){
cin>>givenarray[i];
}
countsort(givenarray);
for(int i=0;i<n;i++){
cout<<givenarray[i];
}
}