-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount Sort.cpp
More file actions
38 lines (30 loc) · 804 Bytes
/
Count Sort.cpp
File metadata and controls
38 lines (30 loc) · 804 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
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin>>n;
int arr[100001]; int k=INT_MIN;
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n;i++){
k=max(arr[i],k);
}
int count[100]={0};
for(int i=0;i<n;i++){
count[arr[i]]++;
}
for(int i=1;i<=k;i++){
count[i]+=count[i-1];
}
int output[n];
for(int i=n-1;i>=0;i--){
output[--count[arr[i]]]=arr[i];
}
for(int i=0;i<n;i++){
arr[i]=output[i];
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
} // https://www.youtube.com/watch?v=imqr13aIBAY