-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bucket_Sort.cpp
56 lines (46 loc) · 1.1 KB
/
Bucket_Sort.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>
#include<bits/stdc++.h>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
float arr[10] ;
int i,n;
cout<< "Enter the number of elements :\n";
cin>>n;
cout<<"Enter the elements:\n";
for (i=0;i<n;i++)
{
cin>>arr[i];
}
//sorting arr[] of size n using bucket sort
std::vector<float> bucket[n];// Create n empty buckets
//Put values in different buckets
for (int i=0; i<n; i++)
{
int bi = n*arr[i]; // Index in bucket
bucket[bi].push_back(arr[i]);
}
//Sort individual buckets
for (int i=0; i<n; i++)
sort(bucket[i].begin(), bucket[i].end());
int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < bucket[i].size(); j++)
arr[index++] = bucket[i][j];
cout << "Sorted array is \n";
for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}
// INPUT: Enter the number of elements :
//4
//Enter the elements:
//0.123
//0.008
//0.7
//0.4
//OUTPUT:Sorted array is
//0.008 0.123 0.4 0.7