-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountingSort.cpp
67 lines (47 loc) · 1.5 KB
/
CountingSort.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Time complexity : O(range + n )
#include <bits/stdc++.h>
using namespace std;
// the '&' is a pointer, so the vector will be updated in main function as well
void countSort(vector <int> &input)
{
int max = *max_element(input.begin(), input.end()); //find the max
int min = *min_element(input.begin(), input.end()); //find the min
int range = max - min + 1; // range is set here
vector<int> count(range);
vector<int> output(input.size());
for(int i = 0; i < input.size(); i++)
count[input[i]-min]++;
for(int i = 1; i < count.size(); i++)
count[i] += count[i-1]; //find the cumulative
for(int i = input.size()-1; i >= 0; i--)
{
output[count[input[i]-min] -1] = input[i];
count[input[i]-min]--; // after every correct placement, decrease count by -1
}
// output is a temp vector which copies back to original vector
for(int i=0; i < input.size(); i++)
input[i] = output[i];
}
//void printArray(vector <int> &arr)
//{
// for (int i=0; i < arr.size(); i++)
// cout << arr[i] << " ";
// cout << "\n";
//}
int main(void)
{
int n;
vector<int> input;
cin >> n;
for(int i=0; i<n; i++){
int value;
cin >>value;
input.push_back(value);
}
countSort(input);
for(auto &v: input){
cout << v << " ";
}
//printArray (arr);
return 0;
}