-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_sort.cpp
76 lines (60 loc) · 1.24 KB
/
bucket_sort.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
68
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
#include <climits>
#include <cassert>
#include <cmath>
#include "bucket_sort.hpp"
#include "insertion_sort.hpp"
#include "my_tools.hpp"
using namespace std;
void bucket_sort(vector<double> &a)
{
int n = a.size();
vector<vector<double> > b(n);
for (int i = 0; i < n; i++) {
b[n*a[i]].push_back(a[i]);
}
for (int i = 0; i < (int)b.size(); i++) {
insertion_sort(b[i]);
}
int cnt = 0;
for (int i = 0; i < (int)b.size(); i++) {
for (int j = 0; j < (int)b[i].size(); j++) {
a[cnt++] = b[i][j];
}
}
}
void bucket_sort2(vector<double> &a)
{
int n = a.size();
vector<vector<double> > b(n);
for (int i = 0; i < n; i++) {
b[n*a[i]].push_back(a[i]);
}
int cnt = 0;
for (int i = 0; i < (int)b.size(); i++) {
for (int j = 0; j < (int)b[i].size(); j++) {
a[cnt++] = b[i][j];
}
}
insertion_sort(a);
}
void test_bucket_sort()
{
for (int i = 0; i < 100; i++) {
int n = rand_int(1, 100000);
printf("n = %d\n", n); // alex
vector<double> a(n);
for (int j = 0; j < n; j++) {
a[j] = rand_double(0, 0.999);
}
vector<double> b = a;
bucket_sort(a);
bucket_sort2(b);
for (int j = 1; j < n; j++) {
assert(a[j] >= a[j-1]);
assert(b[j] >= b[j-1]);
}
printf("\n"); // alex
}
}