-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.1.1.cpp
65 lines (53 loc) · 1.05 KB
/
20.1.1.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
#include "bits/stdc++.h"
using namespace std;
void initializeWithZero(int arr[], int n){
for(int i=0; i<n; i++){
arr[i] = 0;
}
}
void display(int arr[], int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
void countSort(int arr[], int n){
int k=arr[0];
for(int i=0; i<n; i++){
k = max(k, arr[i]);
}
k += 1;
// Initialize Count Array With Zero
int count[k];
initializeWithZero(count, k);
for(int i=0; i<n; i++){
int item = arr[i];
count[item]++;
}
for(int i=1; i<n; i++){
count[i] += count[i-1];
}
int output[n];
for(int i=n-1; i>=0; i--){
int item = arr[i];
int pos = --count[item];
output[pos] = item;
}
for(int i=0; i<n; i++){
arr[i] = output[i];
}
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
countSort(arr, n);
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}