Quick Sort Master Theorem Time Complexity Analysis and Space Complexity Analysis
Quick Sort Master Theorem Time Complexity Analysis and Space Complexity Analysis
Faculty of Engineering,
𝟐𝒏𝒅 Year Computer and Systems Department
Quick Sort Recurrence Relation:
1-We pick a pivot and partition the array into two subarrays.
2-We recursively sort the left and right subarrays.
f(n) is the cost of work done outside the recursive calls (like
a=2: We divide the array into two parts (left and right).
b=2: Each part is roughly half the size of the original array in the
average case.
f(n)=O(n): This is the cost of partitioning the array around the pivot.
In the worst case (e.g., if the array is already sorted and we always
choose the first element as the pivot), Quick Sort will split the array into
parts of size n−1 and 0 instead of equal halves. The recurrence becomes:
T(n)=T(n−1)+O(n)
This recurrence does not fit the form required for the Master Theorem.
However, it can be solved by unrolling the recurrence:
T(n)=T(n−1)+n=T(n−2)+(n−1)+n=⋯=T(1)+(2+3+⋯+n)=O(n2)
Space Complexity:
Summary