forked from CodersForLife/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_sort.cpp
More file actions
44 lines (35 loc) · 877 Bytes
/
Copy pathcount_sort.cpp
File metadata and controls
44 lines (35 loc) · 877 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
39
40
41
42
43
44
//Counting sort works when the input is known to be withing a range.
#include <iostream>
using namespace std;
const int INPUT_SIZE = 20; //total count of input numbers
const int BUCKET_K = 10; //range of numbers will be from 1 to 10
void print(int *input)
{
for ( int i = 0; i < INPUT_SIZE; i++ )
cout << input[i] << " ";
cout << endl;
}
void countingsort(int* input)
{
int CountArr[BUCKET_K] = { 0 };
for (int i=0;i<INPUT_SIZE;i++)
{
CountArr[input[i]]++;
}
int outputindex=0;
for (int j=0;j<BUCKET_K;j++)
{
while (CountArr[j]--)
input[outputindex++] = j;
}
}
int main()
{
int input[INPUT_SIZE] = { 2, 4, 6, 4, 7, 1, 4, 9, 5, 3, 7, 2, 2, 6, 9, 3, 7, 3, 4, 4 };
cout << "Input: ";
print(input);
countingsort(input);
cout << "Output: ";
print(input);
return 0;
}