Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bucket sort #744

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions sorting/Bucket_Sort/bucket_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void bucketSort(float arr[], int n)
{
vector<float> b[n];
for (int i = 0; i < n; i++) {
int bi = n * arr[i];
b[bi].push_back(arr[i]);
}
for (int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
}
int main()
{
float arr[]
= {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434} ;
int n = sizeof(arr) / sizeof(arr[0]);
bucketSort(arr, n);
cout << "Sorted array is \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
29 changes: 29 additions & 0 deletions sorting/bucket_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void bucketSort(float arr[], int n)
{
vector<float> b[n];
for (int i = 0; i < n; i++) {
int bi = n * arr[i];
b[bi].push_back(arr[i]);
}
for (int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
}
int main()
{
float arr[]
= {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434} ;
int n = sizeof(arr) / sizeof(arr[0]);
bucketSort(arr, n);
cout << "Sorted array is \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}