Answer to Question #274796 in C for n hasan

Question #274796

Given an array array[], its starting position l and its ending position r. Sort the


array using Bucket Sort algorithm.



Input: N = 10


array[] = {10 9 7 8 6 2 4 3 5 1}


Output: 1 2 3 4 5 6 7 8 9 10



1
Expert's answer
2021-12-02T14:15:36-0500
#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 bi = n * arr[i]; // Index in bucket
		b[bi].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];
}

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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

joy
10.12.21, 07:46

like

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS