forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounting-sort.cpp
38 lines (30 loc) · 819 Bytes
/
counting-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
#include <iostream>
using namespace std;
int main()
{
static int array[]{4,12,56,754,1235,435,64,1,2,3,4};
int max = array[0];
for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++) {
if (array[i] > max) {
max = array[i];
}
}
int bookkeeping[max];
for (int i = 0; i < sizeof(bookkeeping)/sizeof(bookkeeping[0])+1; i++) {
bookkeeping[i] = 0;
}
for(int i : array) {
bookkeeping[i] = bookkeeping[i]+1;
}
int count = 0;
for (int i = 0; i < sizeof(bookkeeping)/sizeof(bookkeeping[0])+1; i++) {
if (bookkeeping[i] != 0) {
array[count++] = i;
bookkeeping[i]--;
i--;
}
}
for (int i = 0; i < sizeof(array)/sizeof(array[0]);i++) {
cout << array[i] << " ";
}
}