-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBucket-sort.cpp
52 lines (49 loc) · 1.61 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
// If input in uniformly dustributred
// like sorting a float numbers in """linear time"""
// don't use:
// 1. comparision sort (mergesort,heapsort,Quick sort)
// - they can peroform better than O(nlogn)
// 2. counting sort
// - because it uses keys as index (float numbers cannot be
// used)
// use:
// Bucker Sort
// bucketSort(arr[], n)
// 1) Create n empty buckets (Or lists).
// 2) Do following for every array element arr[i].
// .......a) Insert arr[i] into bucket[n*array[i]]
// 3) Sort individual buckets using insertion sort.
// 4) Concatenate all sorted buckets.
#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 index = n * arr[i];
b[index].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];
}
}
}
void PrintArray(float *arr, int n) {
cout << "Sorted array is \n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
std::cout << std::endl;
}
int main(int argc, char const *argv[]) {
float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
int n = sizeof(arr) / sizeof(arr[0]);
bucketSort(arr, n);
PrintArray(arr, n);
return 0;
}