0% found this document useful (0 votes)
2 views10 pages

AOA ExpT 3

The document outlines an academic experiment on the Quick Sort algorithm conducted by a student at Vidyavardhini’s College of Engineering and Technology. It details the algorithm's theory, complexity analysis, and implementation, emphasizing its divide-and-conquer strategy and average time complexity of O(n log n). The experiment aims to enhance understanding of algorithm design and analysis, demonstrating Quick Sort's efficiency compared to other sorting methods.

Uploaded by

ashcraftgamer8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

AOA ExpT 3

The document outlines an academic experiment on the Quick Sort algorithm conducted by a student at Vidyavardhini’s College of Engineering and Technology. It details the algorithm's theory, complexity analysis, and implementation, emphasizing its divide-and-conquer strategy and average time complexity of O(n log n). The experiment aims to enhance understanding of algorithm design and analysis, demonstrating Quick Sort's efficiency compared to other sorting methods.

Uploaded by

ashcraftgamer8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Vidyavardhini’s College of Engineering and Technology

Department of Computer Engineering


Academic Year: 2023-24 (Even Sem)

Experiment No. 3
Quick Sort
Date of Performance:04/02/2025
Date of Submission:11/02/2025
Name: Yash Gade
Div: SE - 3 Batch: A Roll no. 07

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

Experiment No. 3
Aim: To study and implement Quick Sort Algorithm

Objective: To introduce the methods of designing and analysing algorithms

Theory:

Quick sort/ Partition exchange sort:

• Partition-exchange sort or quicksort algorithm was developed in 1960 by Tony Hoare.

• Quick sort uses divide and conquer strategy.

• Quick sort picks one element as a pivot and shifts the pivot to the correct location in
each iteration.

• By repositioning the pivot, the list is divided into two sub-lists of equal or unequal
size.

• Each sub-list is solved in a recursive manner.

• The value of the pivot, not the position, determines division.

• As a pivot, the first, last, or any random element can be chosen, however, the pivot
position should remain constant throughout all the recursive calls.

• Quick sort algorithm on average, makes O(n log n) comparisons to sort n items. In the
worst case, it makes O(n2) comparisons.

• Quicksort is a comparison sort and, in efficient implementations, is not a stable sort.

• Quicksort can be implemented with an in-place partitioning algorithm, so the entire sort
can be done with only O(log n) additional space used by the stack during the recursion.

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

Algorithm:

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

Complexity of Quick Sort:

Best Case:

• Occurs when the pivot divides the array into roughly equal-sized sub-arrays
consistently leading to a logarithmic complexity.

• The algorithm exhibits O(n log n) time complexity in best case.

Recurrence Relation:

T(n) = 0 for n=1

T(n) = 2T(n/2) + O(n) for n>1…(1)

• 2T(n/2) is the time required to solve two problems of size (n/2)

• O(n) is the time required to fix the position of the pivot. Fixing of pivot requires a scan
of the entire array.

• The recurrence of the quicksort for the best case is identical to the recurrence of merge
sort.

• Depending upon how partitioning is done, running time of quick sort varies form
O(nlog2n) to O(n2).

• Best case occurs when partitioning happens from the center of list on each call. In this
case, quick sort resembles to merge sort.

Solve Recurrence relation using Substitution Method:

T(n) = 0 for n=1

T(n) = 2T(n/2) + O(n) for n>1…(1)

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

Solution:

Solving original recurrence for n/2,

T(n/2) = 2T(n/4) + n/2

Substituting this in equation (1),

T(n) = 2[ 2T(n/4) + n/2 ] + n

= 22 T(n/22) + 2n

.....

After k substitutions,

T(n) = 2k T(n/2k) + k.n … (2)

• Division of array creates binary tree, which has height log2n.

• Let us consider that k grows up to log2n,

k = log2n ⇒ n = 2k

Substitute these values in equation (2)

T(n) = nT(n/n) + log2n . n

T(n) = O(n.log2n)

(As, T(1) = 0, from base case)

Worst Case: List has already been sorted.

• The pivot will be at either end in such a scenario.

• Quick sort’s worst-case behavior generates subproblems with (n – 1) elements and


zero elements.

• In each call sets the right position of the pivot and no further work is required, the
combination cost for quicksort is constant.

• The method scans the array and identifies the right location of the pivot to split the list,
therefore the division cost of QUICK SORT is linear, i.e. O(n).

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

• In the worst-case scenario, one of the sublists has size 0 and the other has size (n – 1),
thus the algorithm must solve a problem of size (n – 1) in the subsequent
recursive call.

Solve recurrence relation:


T(0) = 1 (Base case)
T(n) = T(conquer) + T(divide) +
T(combine)
T (n) = T (n – 1) + O(n) + O(1)
= T (n – 1) + n …(1)
Substitute n by (n – 1) in Equation (1),
T (n – 1) = T (n – 2) + (n – 1) …(2)
T (n) = T (n – 2) + (n – 1) + n …(3)
Substitute n by (n – 1) in Equation (2),
T (n – 2) = T (n – 3) + (n – 2)
From Equation (3),
T (n) = T (n – 3) + (n – 2) + (n – 1) + n
.....
After k iterations,
T(n) = T(n – k) + (n – k + 1) + (n – k + 2)
+ … + (n – 1) + n
Let k = n,
T (n) = T (0) + 1 + 2 + 3 + . . . + (n – 1) + n
= 1 + 2 + 3 + …+ n
= n(n + 1) / 2
= (n2/2) + (n/2)
= O(n2)
Note : In the worst case, quick sort
behaves similar to selection sort.

Average case:
For quick sort, average-case running time is much closer to the best case.
That is, T (n) = O(n log2n)

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

Solved Example: Sort the elements given in the following array using quick sort algorithm

Now left = loc, so the procedure terminates, as the pivot element (the first element of
the array, that is, 27) is placed in its correct position.

All the elements smaller than 27 are placed before it and those greater than 27 are
placed after it.

The left sub-array containing 25, 10, 18 and the right sub-array containing 36 and 45
are sorted in the same manner

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

Question: Solve the following list of elements using Quick Sort:


66 22 44 11 88 33 77 55 99
Program to implement Quick Sort:
#include <stdio.h>
#include <conio.h>

#define MAX 10

void quickSort(int a[], int n);


int partition(int a[], int f, int l);

void main() {
clrscr();

int a[MAX], n, i;

printf("Enter the size of array: ");


scanf("%d", &n);

printf("Enter the array elements: ");


for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

quickSort(a, n);

printf("\nAfter Sorting: ");


for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}

getch();
}

void quickSort(int a[], int n) {


int f = 0, l = n - 1, p;

while (f < l) {
p = partition(a, f, l);

if (p - 1 > f) {
l = p - 1;
} else {

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

f = p + 1;
}
}
}

int partition(int a[], int f, int l) {


int pivot = a[f];
int i = f + 1;
int j = l;
int temp;

while (i <= j) {
while (i <= l && a[i] <= pivot) i++;
while (a[j] > pivot) j--;

if (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

temp = a[f];
a[f] = a[j];
a[j] = temp;

return j;
}

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

Output:

Conclusion: In this experiment, the Quick Sort algorithm was studied and
implemented to understand its design and analysis. Quick Sort is a divide-and-conquer
algorithm that works by selecting a pivot element and partitioning the array into two
sub-arrays: elements smaller than the pivot and elements greater than the pivot. These
sub-arrays are then recursively sorted. The objective was to explore the method of
designing efficient algorithms and analyzing their performance. Quick Sort, with an
average time complexity of O(n log n), was found to be more efficient than other
sorting algorithms like Insertion and Selection Sort, especially for large datasets. The
experiment provided insights into the importance of choosing the right pivot and
understanding the algorithm's behavior in different cases.

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering

You might also like