forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket_sort.cpp
More file actions
41 lines (32 loc) · 786 Bytes
/
bucket_sort.cpp
File metadata and controls
41 lines (32 loc) · 786 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
37
38
39
40
41
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void bucketSort(float a[], int n)
{
vector<float> t[n];
for (int i=0; i<n; i++)
{
int x = n*a[i];
t[x].push_back(a[i]);
}
for (int i=0; i<n; i++)
sort(t[i].begin(), t[i].end());
int k= 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < t[i].size(); j++)
a[k++] = t[i][j];
}
int main()
{
float a[] = {0.765, 0.324, 0.111, 0.951, 0.245, 0.48};
int n = sizeof(a)/sizeof(a[0]);
cout << "Elements before Sorting\n";
for (int i=0; i<n; i++)
cout << a[i] << " ";
bucketSort(a, n);
cout << "\nElements After Sorting \n";
for (int i=0; i<n; i++)
cout << a[i] << " ";
return 0;
}