forked from sahilbansalweb/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucketSort.cpp
More file actions
36 lines (34 loc) · 918 Bytes
/
BucketSort.cpp
File metadata and controls
36 lines (34 loc) · 918 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void bucketSort(vector<float> &A)
{
vector<float> buckets[A.size()];
// Putting each element into appropriate bucket
for (int i = 0; i < A.size(); i++)
{
int bucketIndex = A[i] * A.size();
buckets[bucketIndex].push_back(A[i]);
}
// Sorting elements in each bucket
for (int i = 0; i < A.size(); i++)
sort(buckets[i].begin(), buckets[i].end());
int i = 0;
// Concatenating all the sorted buckets
for (int j = 0; j < A.size(); j++)
for (int b = 0; b < buckets[j].size(); b++)
{
A[i] = buckets[j][b];
i++;
}
}
int main()
{
vector<float> A = {0.32, 0.44, 0.31, 0.37, 0.17, 0.51, 0.18, 0.16, 0.17, 0.11};
bucketSort(A);
for (int i = 0; i < A.size(); i++)
cout << A[i] << ' ';
cout << endl;
return 0;
}