-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathBucketSort.cpp
49 lines (38 loc) · 1010 Bytes
/
BucketSort.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
// Bucket Sort in cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print_vec(vector<float> vec) {
for(int i = 0; i<vec.size(); i++) {
cout << vec[i] << " ";
}
cout << "\n";
}
void bucketSort(vector<float>&vec) {
int size = vec.size();
vector<float> bucket[size];
// put elements into different buckets
for(int i = 0; i<size; i++) {
bucket[int(size * vec[i])].push_back(vec[i]);
}
//sort individual vectors
for(int i = 0; i<size; i++) {
sort(bucket[i].begin(), bucket[i].end());
}
int index = 0;
for(int i = 0; i < size; i++) {
for (int j = 0; j < bucket[i].size(); j++) {
vec[index++] = bucket[i][j];
}
}
}
int main() {
vector<float> v(10);
v = {0.542, 0.512, 0.761, 0.123, 0.541, .0981, 0.841, 0.01, 0.7, 0.901};
cout << "Array before Sorting: ";
print_vec(v);
bucketSort(v);
cout << "Array after Sorting: ";
print_vec(v);
}