forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSort
More file actions
38 lines (37 loc) · 714 Bytes
/
countSort
File metadata and controls
38 lines (37 loc) · 714 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<iostream>
#include<bits/stdc++.h>
using namespace std;
void countSort(int arr[],int n){
int k=arr[0];
for(int i=0; i<n; i++){
k= max(k,arr[i]);
}
int count[k+1]={0};
for(int i=0; i<n; i++){
count[arr[i]]++;
}
int output[n];
for(int i=1; i<=k; i++){
count[i]+=count[i-1];
}
for(int i=n-1; i>=0; i--){
count[arr[i]]-=1;
output[count[arr[i]]]= arr[i];
}
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]<<" ";
}
return 0;
}