-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_preview_main
59 lines (52 loc) · 876 Bytes
/
01_preview_main
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
#include <bits/stdc++.h>
using namespace std;
int findmax(int *a, int n)
{
int m = INT_MIN;
for (int i = 0; i < n; i++)
{
m = max(m, a[i]);
}
return m;
}
// Exam over
// Over
// Updated on leethub
void countsort(int a[], int n)
{
int *c, i, j, mx;
mx = findmax(a, n);
c = new int[mx + 1];
for (i = 0; i < mx + 1; i++)
{
c[i] = 0;
}
for (i = 0; i < n; i++)
{
c[a[i]]++;
}
i = j = 0;
while (j < mx + 1)
{
if (c[j] > 0)
{
a[i++] = j;
c[j]--;
}
else
{
j++;
}
}
}
int main()
{
int arr[] = {7, 74, 26, 23, 45, 27, 35, 77, 0, 0, 54, 33, 12, 45, 43}, n = 15;
countsort(arr, n);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}