-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBucketSorting.cpp
More file actions
50 lines (40 loc) · 1.19 KB
/
Copy pathBucketSorting.cpp
File metadata and controls
50 lines (40 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
45
46
47
48
49
50
#include <vector>
#include <iostream>
void bucket_sorting(std::vector<int>& data)
{
if (data.size() < 2)
return;
auto min = std::numeric_limits<int>::max();
auto max = std::numeric_limits<int>::min();
for (const auto value: data) {
min = std::min(min, value);
max = std::max(max, value);
}
const auto range = max - min;
const auto buckets_number = data.size() / 2;
std::vector<std::vector<int>> buckets(buckets_number + 1);
for (const auto value: data) {
const auto index = value * buckets_number / range;
buckets.at(index).push_back(value);
}
for (auto& bucket: buckets)
std::sort(begin(bucket), end(bucket));
size_t index = 0;
for (const auto& bucket: buckets) {
for (const auto value: bucket) {
data[index++] = value;
}
}
}
int main()
{
std::vector<int> vector = { 4, 7, 1, 5, 2, 9, 4, 7, 2, 9, 4 };
std::cout << "Not sorted array: ";
for (const auto& value : vector)
std::cout << value << " ";
bucket_sorting(vector);
std::cout << "\nSorted array: ";
for (const auto& value : vector)
std::cout << value << " ";
return EXIT_SUCCESS;
}