Assignment 7
Assignment 7
CODE:
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
using namespace std;
using namespace std::chrono;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
}
void bucketSort(int arr[], int n) {
int max = getMax(arr, n);
vector<int> buckets[n];
for (int i = 0; i < n; i++) {
int bi = (n * arr[i]) / (max + 1); // Index in bucket
buckets[bi].push_back(arr[i]);
}
for (int i = 0; i < n; i++)
sort(buckets[i].begin(), buckets[i].end()); int
index = 0;
int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
int brr[n];
for (int i = 0; i < n; i++)
brr[i] = arr[i];
printArray(arr, n);
cout << "Time taken by Radix Sort: " << duration.count() << "
microseconds\n";
start = high_resolution_clock::now();
bucketSort(brr, n);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
cout << "Sorted array using Bucket Sort: \n";
printArray(brr, n);
cout << "Time taken by Bucket Sort: " << duration.count() << "
microseconds\n";
return 0;
}
OUTPUT:
CODE:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
OUTPUT: