Prac-6 Darshan ADA Gtu
Prac-6 Darshan ADA Gtu
Experiment No: 6
Implement a program for randomized version of quick sort and compare its performance with the
normal version of quick sort using steps count on various inputs (1000 to 5000) ofrandom nature,
ascending order and descending order sorted data.Also, draw a comparative chart of number of
input versus steps executed/time taken for each cases (random, ascending, and descending).
Date:
Theory:
In terms of performance comparison, we graph input size versus both the number of steps
executed and time taken. For random data, the two algorithms are comparable up to around
3000 elements, beyond which basic quicksort begins to lag. For already sorted or reverse-
sorted data, the basic version's performance worsens considerably as input size increases,
making more steps and taking more time due to its poor pivot choices. In contrast,
randomized quicksort maintains consistent performance across all cases, demonstrating that
its random pivot selection helps it handle worst-case scenarios more gracefully. This
comparison highlights that randomized quicksort is a more robust option, particularly when
input data is large and potentially ordered.
Implement a function of randomized version of quick sort as per above instructions and use
basic version of quick sort (that selects first element as pivot element). Compare the steps
count of both the functions on various inputs ranging from 1000 to 5000 for each case
(random, ascending, and descending).
1
Analysis and Design of Algorithms (3150703)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Swap function
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
// Normal quicksort
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);
}
}
// Randomized quicksort
void randomizedQuickSort(int arr[], int low, int high) {
if (low < high) {
int pi = randomizedPartition(arr, low, high);
randomizedQuickSort(arr, low, pi - 1);
2
Analysis and Design of Algorithms (3150703)
randomizedQuickSort(arr, pi + 1, high);
}
}
int main() {
srand(time(0)); // Seed for randomization
int n[] = {1000, 2000, 3000, 4000, 5000}; // Different input sizes
int size = sizeof(n) / sizeof(n[0]);
// Random data
generateRandomArray(arr, len);
copyArray(arr, copy, len);
steps = 0;
clock_t start = clock();
quickSort(copy, 0, len - 1);
clock_t end = clock();
double normal_time_random = ((double)(end - start)) / CLOCKS_PER_SEC;
long long normal_steps_random = steps;
3
Analysis and Design of Algorithms (3150703)
long long randomized_steps_random = steps;
return 0;
}
4
Analysis and Design of Algorithms (3150703)
Output:
Observations:
Randomized QuickSort generally outperforms the normal version, especially in cases of sorted
(ascending or descending) data where normal QuickSort struggles due to poor pivot selection, leading
to more comparisons and swaps. Randomized QuickSort avoids this by selecting a random pivot,
distributing partition sizes more evenly and improving performance. On random input, both
algorithms perform similarly, but the randomized version tends to reduce the likelihood of worst-case
scenarios, offering more consistent efficiency. Overall, Randomized QuickSort is more robust and
efficient across various input types and sizes.
5
Analysis and Design of Algorithms (3150703)
Result: Complete the below table based on your implementation of sequential search algorithm and
steps executed by the function.
Prepare similar tables for descending order and ascending order sorted data.
6
Analysis and Design of Algorithms (3150703)
Chart:
Conclusion:
Randomized QuickSort is more efficient and reliable than Normal QuickSort, especially for sorted
data, as it reduces the chances of worst-case performance through random pivot selection.
Quiz:
2. What is the time complexity of basic version of Quick Sort on sorted data? Give reason of
your answer.
Answer:
7
Analysis and Design of Algorithms (3150703)
3. Can we always ensure O(n*lg n) time complexity for Randomized Quick Sort?
Answer:
Suggested Reference:
1. "Introduction to Algorithms" by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest,
and Clifford Stein
2. “Fundamentals of Algorithms”by E. Horowitz et al.
8
Analysis and Design of Algorithms (3150703)