From 43ab07e72e9b7010346b5f60b57d14991c06ede8 Mon Sep 17 00:00:00 2001 From: Archita Bhattad <87604491+ARCHITABHATTAD@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:32:20 +0530 Subject: [PATCH] Create BucketSort.cpp --- BucketSort.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 BucketSort.cpp 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; +}