Skip to content
Open
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
44 changes: 44 additions & 0 deletions Bucket_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

// C++ program to sort an array using bucket sort by Jukta Goyari
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// Function to sort arr[] of size n using bucket sort
void bucketSort(float arr[], int n)
{

// Create n empty buckets
vector<float> b[n];

// Put array elements in different buckets
for (int i = 0; i < n; i++) {
int bi = n * arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}

// Sort individual buckets
for (int i = 0; i < n; i++)
sort(b[i].begin(), b[i].end());

// Concatenate all buckets into arr[]
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
}

/* Driver program to test above function */
int main()
{
float arr[]
= { 0.897, 0.565, 0.766, 0.1234, 0.665, 0.3438 };
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;
}