AOA ExpT 3
AOA ExpT 3
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
Experiment No. 3
Aim: To study and implement Quick Sort Algorithm
Theory:
• 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.
• 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 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.
Algorithm:
Best Case:
• Occurs when the pivot divides the array into roughly equal-sized sub-arrays
consistently leading to a logarithmic complexity.
Recurrence Relation:
• 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.
Solution:
= 22 T(n/22) + 2n
.....
After k substitutions,
k = log2n ⇒ n = 2k
T(n) = O(n.log2n)
• 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).
• 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.
Average case:
For quick sort, average-case running time is much closer to the best case.
That is, T (n) = O(n log2n)
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
#define MAX 10
void main() {
clrscr();
int a[MAX], n, i;
quickSort(a, n);
getch();
}
while (f < l) {
p = partition(a, f, l);
if (p - 1 > f) {
l = p - 1;
} else {
f = p + 1;
}
}
}
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;
}
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.