forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.cpp
51 lines (38 loc) · 1.1 KB
/
count.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void countingSort(vector<int>& arr) {
if (arr.empty()) return;
// Find the maximum value in the array
int maxVal = *max_element(arr.begin(), arr.end());
// Create a count array to store the count of each unique value
vector<int> count(maxVal + 1, 0);
// Count each element in the original array
for (int num : arr) {
count[num]++;
}
// Modify the count array to store the cumulative count
for (int i = 1; i <= maxVal; i++) {
count[i] += count[i - 1];
}
// Create a sorted output array
vector<int> output(arr.size());
// Build the output array
for (int i = arr.size() - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
// Copy the sorted elements back to the original array
arr = output;
}
int main() {
vector<int> arr = {4, 2, 2, 8, 3, 3, 1};
countingSort(arr);
cout << "Sorted array: ";
for (int num : arr) {
cout << num << " ";
}
cout << endl;
return 0;
}