Quick Sort - Program 10
Quick Sort - Program 10
Design and implement C/C++ Program to sort a given set of n integer elements
using Quick Sort method and compute its time complexity. Run the program for
varied values of n> 5000 and record the time taken to sort. Plot a graph of the
time taken versus n. The elements can be read from a file or can be generated
using the random number generator.
Aim: The aim of this program is to sort ‘n’ randomly generated elements using
Quick sort and Plotting the graph of the time taken to sort n elements versus n.
Definition: Quick sort is based on the Divide and conquer approach. Quick sort
divides array according to their value. Partition is the situation where all the elements
before some position s are smaller than or equal to A[s] and all the elements after
position s are greater than or equal to A[s].
Efficiency: Cbest(n) Є Θ(nlog2n),Cworst(n) ЄΘ(n2),Cavg(n)Є1.38nlog2n
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
int t = *a;
*a = *b;
*b = t;
int main() {
FILE *fp;
for (;;) {
scanf("%d", &iChoice);
switch (iChoice) {
case 1:
fp = fopen("QuickPlot.dat", "w");
if (fp == NULL) {
exit(1);
printf("\nGenerating data...\n");
fnGenRandInput(iaArr, i);
gettimeofday(&tv, NULL);
fnQuickSort(iaArr, 0, i - 1);
gettimeofday(&tv, NULL);
fclose(fp);
printf("\nData File generated and stored in file < QuickPlot.dat >.\n Use a plotting utility\n");
break;
case 2:
printf("\nUnsorted Array\n");
fnGenRandInput(iaArr, iNum);
fnDispArray(iaArr, iNum);
printf("\nSorted Array\n");
fnDispArray(iaArr, iNum);
break;
case 3:
exit(0);
return 0;
int i, j, temp;
int p;
p = a[l];
i = l;
j = r + 1;
do {
do {
i++;
do {
j--;
fnSwap(&a[i], &a[j]);
fnSwap(&a[l], &a[j]);
return j;
int s;
if (l < r) {
s = fnPartition(a, l, r);
fnQuickSort(a, l, s - 1);
fnQuickSort(a, s + 1, r);
int i;
srand(time(NULL));
int i;
set grid