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
Algorithm: Quick sort (A[l….r])
// Sorts a sub array by quick sort
//Input : A sub array A[l..r] of A[0..n-1] ,defined by its left and right indices
l
//and r
// Output : The sub array A[l..r] sorted in non decreasing order
if l < r
s = Partition (A[l..r]) //s is a split position
Quick sort (A[l …s-1])
Quick sort (A[s+1…r])
ALGORITHM Partition (A[l…r])
//Partition a sub array by using its first element as a pivot
// Input : A sub array A[l…r] of A[0…n-1] defined by its left and right
indices l and // r (l < r)
// Output : A partition of A[l…r], with the split position returned as this
function’s value
p=A[l]
i=l;
j=r+1;
repeat
delay(500);
repeat i= i+1 until A[i] >= p
repeat j=j-1 until A[J] <= p
Swap (A[i],A[j])
until I >=j
Swap (A[i],A[j]) // Undo last Swap when i>= j
Swap (A[l],A[j])
Return j
Program : Quick Sort
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
void fnGenRandInput(int [], int);
void fnDispArray(int [], int);
int fnPartition(int [], int, int);
void fnQuickSort(int [], int, int);
inline void fnSwap(int*, int*);
inline void fnSwap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
int main() {
FILE *fp;
struct timeval tv;
double dStart, dEnd;
int iaArr[50000], iNum, iPos, iKey, i, iChoice;
for (;;) {
printf("\n1.Plot the Graph\n2.QuickSort\n3.Exit");
printf("\nEnter your choice\n");
scanf("%d", &iChoice);
switch (iChoice) {
case 1:
printf("\nBefore file creation\n");
fp = fopen("QuickPlot.dat", "w");
if (fp == NULL) {
perror("Error opening file QuickPlot.dat");
printf("Error: Unable to create QuickPlot.dat file for writing.\n");
exit(1);
printf("File created successfully\n");
printf("\nGenerating data...\n");
for (i = 100; i < 10000; i += 100) {
fnGenRandInput(iaArr, i);
gettimeofday(&tv, NULL);
dStart = tv.tv_sec + (tv.tv_usec / 1000000.0);
fnQuickSort(iaArr, 0, i - 1);
gettimeofday(&tv, NULL);
dEnd = tv.tv_sec + (tv.tv_usec / 1000000.0);
fprintf(fp, "%d\t%lf\n", i, dEnd - dStart);
fclose(fp);
printf("\nData File generated and stored in file < QuickPlot.dat >.\n Use a plotting utility\n");
break;
case 2:
printf("\nEnter the number of elements to sort\n");
scanf("%d", &iNum);
printf("\nUnsorted Array\n");
fnGenRandInput(iaArr, iNum);
fnDispArray(iaArr, iNum);
fnQuickSort(iaArr, 0, iNum - 1);
printf("\nSorted Array\n");
fnDispArray(iaArr, iNum);
break;
case 3:
exit(0);
return 0;
int fnPartition(int a[], int l, int r) {
int i, j, temp;
int p;
p = a[l];
i = l;
j = r + 1;
do {
do {
i++;
} while (a[i] < p);
do {
j--;
} while (a[j] > p);
fnSwap(&a[i], &a[j]);
while (i < j);
fnSwap(&a[i], &a[j]);
fnSwap(&a[l], &a[j]);
return j;
void fnQuickSort(int a[], int l, int r) {
int s;
if (l < r) {
s = fnPartition(a, l, r);
fnQuickSort(a, l, s - 1);
fnQuickSort(a, s + 1, r);
void fnGenRandInput(int X[], int n) {
int i;
srand(time(NULL));
for (i = 0; i < n; i++) {
X[i] = rand() % 10000;
void fnDispArray(int X[], int n) {
int i;
for (i = 0; i < n; i++) {
printf(" %5d \n", X[i]);
# Gnuplot script file for plotting data in file "QuickPlot.dat"
# This file is called plot_script.gp
set terminal png
set output 'C:\Users\bhagya\OneDrive\Documents\ADA\Quick sort\QuickPlot.png'
set title 'Time Complexity for Quick Sort'
set xlabel 'Size of Input'
set ylabel 'Sorting Time (microseconds)'
set grid
plot 'C:\Users\bhagya\OneDrive\Documents\ADA\Quick sort\QuickPlot.dat' with lines