0% found this document useful (0 votes)
5 views

Assignment 7

The document discusses Radix sort and Bucket sort algorithms. It provides code to implement Radix sort and Bucket sort on a sample array. It also analyzes the time complexity of Radix sort and Bucket sort by running them on the array and outputting the time taken by each.

Uploaded by

Nilesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 7

The document discusses Radix sort and Bucket sort algorithms. It provides code to implement Radix sort and Bucket sort on a sample array. It also analyzes the time complexity of Radix sort and Bucket sort by running them on the array and outputting the time taken by each.

Uploaded by

Nilesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT-7

Question 1:wap to show the analysis of Radix sort and Bucket


sort?

CODE:

#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
using namespace std;
using namespace std::chrono;

int getMax(int arr[], int n) {


int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}

void countSort(int arr[], int n, int exp) {


int output[n]; // output array

int count[10] = {0};


// Store count of occurrences in count[]
for (int i = 0; i < n; i++)

count[(arr[i] / exp) % 10]++;


for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build the output array
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;

}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}

void radixSort(int arr[], int n) {


int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);

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

for (int i = 0; i < n; i++)


for (int j = 0; j < buckets[i].size(); j++)
arr[index++] = buckets[i][j];

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}

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

auto start = high_resolution_clock::now();


radixSort(arr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Sorted array using Radix Sort: \n";

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:

Question 2:WAPto show the analysis of of randomized partition in


Quick sort??

CODE:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void swap(int* a, int* b) {


int t = *a;
*a = *b;
*b = t;
}

int partition(int arr[], int low, int high) {


srand(time(NULL));
int random = low + rand() % (high - low);
swap(&arr[random], &arr[high]);
int pivot = arr[high];
int i = low - 1; // Index of smaller element
for (int j = low; j <= high - 1; j++) {
if (arr[j] <= pivot) {
i++; // Increment index of smaller element swap(&arr[i],
&arr[j]); // Swap current element with index }
}
swap(&arr[i + 1], &arr[high]); // Swap pivot with index+1
return (i + 1); // Return the partition index
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Unsorted array: \n";
printArray(arr, n);
quickSort(arr, 0, n - 1);
cout << "Sorted array using Quick Sort: \n";
printArray(arr, n);
return 0;
}

OUTPUT:

You might also like