diff --git a/BucketSort.cpp b/BucketSort.cpp new file mode 100644 index 0000000..00b9013 --- /dev/null +++ b/BucketSort.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +using namespace std; + +void bucketSort(float arr[], int n) { + vector b[n]; + for (int i = 0; i < n; i++) b[n * arr[i]].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 (float num : b[i]) arr[index++] = num; +} + +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; +}