-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcount_sort.cpp
52 lines (43 loc) · 848 Bytes
/
count_sort.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
#include<bits/stdc++.h>
//#include<iostream>
using namespace std;
int main(){
// 1 3 2 3 4 1 6 4 3
int n;
cin>>n;
int arr[n],mx=INT_MIN;
for (int i = 0; i < n; i++)
{
cin>>arr[i];
mx=max(mx,arr[i]);
}
mx++;
int count[mx]={0};
for (int i = 0; i < n; i++)
{
(count[arr[i]])++;
}
for (int i = 1; i < mx; i++)
{
count[i]+=count[i-1];
}
int ans[n];
for (int i = n-1; i >=0; i--)
{
ans[--count[arr[i]]]=arr[i];
}
for (int i = 0; i < n; i++)
{
cout<<ans[i]<<" ";
}
// int temp=0;
// for (int i = 0; i < mx; i++)
// {
// for (int j = 0; j < count[i]; j++)
// {
// arr[temp]=i;
// temp++;
// }
// }
return 0;
}