-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07- Count Sort.cpp
More file actions
44 lines (39 loc) · 1.19 KB
/
Copy path07- Count Sort.cpp
File metadata and controls
44 lines (39 loc) · 1.19 KB
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
// In The Name of Allah
// Counting Sort
//
// Problem: Sort an array of non-negative integers when the range of values
// (k) is small relative to the count (n).
// Approach: Count occurrences of each value, take a prefix sum to get
// insertion positions, then write each input element into its
// position in the output (right-to-left for stability).
// Time: O(n + k)
// Space: O(n + k)
// Stable: Yes
//
// Used as the inner step of Radix Sort.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void counting_sort(vector<int>& v) {
if (v.empty()) return;
int mx = *max_element(v.begin(), v.end());
int mn = *min_element(v.begin(), v.end());
int range = mx - mn + 1;
int n = (int)v.size();
vector<int> count(range, 0), output(n);
for (int x : v) count[x - mn]++;
for (int i = 1; i < range; i++) count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[--count[v[i] - mn]] = v[i];
}
v = output;
}
int main() {
vector<int> v = {4, 2, 2, 8, 3, 3, 1};
counting_sort(v);
cout << "Sorted:";
for (int x : v) cout << " " << x;
cout << endl;
return 0;
}