Skip to content

Commit beca753

Browse files
Merge pull request #64 from vaibhav0726/patch-1
Added Bucket Sort Algo
2 parents 3f52128 + dc18f00 commit beca753

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

CPP/sorting/bucketsort.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void bucketSort(float arr[], int n)
5+
{
6+
vector<float> b[n];
7+
for (int i = 0; i < n; i++) {
8+
int bi = n * arr[i];
9+
b[bi].push_back(arr[i]);
10+
}
11+
for (int i = 0; i < n; i++)
12+
sort(b[i].begin(), b[i].end());
13+
int index = 0;
14+
for (int i = 0; i < n; i++)
15+
for (int j = 0; j < b[i].size(); j++)
16+
arr[index++] = b[i][j];
17+
}
18+
19+
int main()
20+
{
21+
float arr[] = { 0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434 };
22+
int n = sizeof(arr) / sizeof(arr[0]);
23+
bucketSort(arr, n);
24+
25+
cout << "Sorted array is \n";
26+
for (int i = 0; i < n; i++)
27+
cout << arr[i] << " ";
28+
return 0;
29+
}

0 commit comments

Comments
 (0)