-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cpp
45 lines (37 loc) · 1.13 KB
/
source.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
// Copyright © Bùi Nguyễn Tấn Sang (EndermanPC) 2024
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
std::vector<int> heavy_sort(const std::vector<int>& arr) {
if (arr.empty()) {
return arr;
}
int min_value = *std::min_element(arr.begin(), arr.end());
int max_value = *std::max_element(arr.begin(), arr.end());
// Step 1: Create a map to act as the bucket
std::unordered_map<int, int> buckets;
// Step 2: Push numbers into the bucket
for (int num : arr) {
buckets[num]++;
}
// Step 3: Create the sorted array
std::vector<int> sorted_arr;
for (int num = min_value; num <= max_value; ++num) {
if (buckets.find(num) != buckets.end()) {
sorted_arr.insert(sorted_arr.end(), buckets[num], num);
}
}
return sorted_arr;
}
// Example usage
int main() {
std::vector<int> arr = {1, 3, 6, 7};
std::vector<int> sorted_arr = heavy_sort(arr);
std::cout << "Sorted array: ";
for (int num : sorted_arr) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}