forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBucketSort.cpp
47 lines (41 loc) · 1.1 KB
/
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
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void BucketSort(vector<float> &array )//function to sort the float array by bucket sort
{
int n=array.size();
vector<float> b[n];//Creates n empty buckets
for(int i=0;i<n;++i)//Categorizes the elements into different buckets
{
int z=n*array[i];
b[z].push_back(array[i]);
}
for(int i=0;i<n;++i)
{
sort(b[i].begin(),b[i].end());//Sorts the invidual buckets
}
int cursor=0;
for(int i=0;i<n;++i)
{
for(int j=0;j<b[i].size();++j)
{
array[cursor]=b[i][j];//Merges back all the sorted buckets
cursor++;
}
}
}
int main()
{
float arr[] = { 0.697, 0.765, 0.456, 0.9234, 0.865, 0.2434 };
//array is predefined here, please take input from the user to work accordingly by modifying the code
int n = sizeof(arr) / sizeof(arr[0]);
vector<float> ar(arr,arr+n);
BucketSort(ar);
cout<<"Float Array after sorting is:\n";
for(int i=0;i<n;++i)
{
cout<<ar[i]<<" ";
}
return 0;
}