Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
abdurrezzak authored Oct 30, 2017
1 parent 73fabe6 commit 9fd51d9
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 11.BucketSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This program takes an array from the user
* sorts it using bucket sort
* Prints the sorted array
* For more information about Bucket Sort Algorithm: https://en.wikipedia.org/wiki/Bucket_sort
*
* Coded by: Abdurrezak EFE
*
* */
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void bucket_sort(float arr[], int k)
{
vector<float> v[k]; //creating buckets

int num;
for(int i=0;i<k;i++)
num = k*arr[i], v[num].push_back(arr[i]); //filling buckets

for(int i=0;i<k;i++)
sort(v[i].begin(),v[i].end()); //sorting every bucket

int index=0;
for(int i=0;i<k;i++)
for(int j=0;j<v[i].size();j++)
arr[index] = v[i][j], index++;

}

int main()
{
int k;
cout << "Enter the number of the floating point numbers you want to construct the array from: ";
cin >> k;

float arr[k];
for(int i=0;i<k;i++)
cin >> arr[i];

bucket_sort(arr, k); //sorting

for(int i=0;i<k;i++) //printing the array
cout << arr[i] << " ";
}

0 comments on commit 9fd51d9

Please sign in to comment.