0% found this document useful (0 votes)
13 views

Quick Sort Master Theorem Time Complexity Analysis and Space Complexity Analysis

Uploaded by

ahmedthomas909
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Quick Sort Master Theorem Time Complexity Analysis and Space Complexity Analysis

Uploaded by

ahmedthomas909
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2024

Quick Sort Time Complexity and Space Complexity Analysis


SUPERVISOR: Eng/ Noor El-Deen Magdy

BY: Ahmed Mamdouh

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.

Master’s theorem can only be applied on decreasing and


dividing recurring functions. If the relation is not
decreasing or dividing, master’s theorem must not be
applied.
T(n) = aT(n/b) + F(n)
Where:
 a is the number of subproblems.

 n/b is the size of each subproblem.

 f(n) is the cost of work done outside the recursive calls (like

combining subproblem results).


The theorem has three cases based on the growth of f(n) relative to
𝒏𝐥𝐨𝐠 𝒃 𝒂 :
1. Case 1: If f(n)=O( 𝑛log𝑏 𝑎−ϵ ) for some ϵ>0, the complexity is
T(n)=Θ(𝑛log𝑏 𝑎 ).
2. Case 2: If f(n)= Θ (𝑛log𝑏 𝑎 ) the complexity is T(n) = Θ(𝑛log𝑏 𝑎 log n)
3. Case 3: If f( n ) = Ω(𝑛log𝑏 𝑎+ϵ ) for some ϵ>0 and a.f(n/b) ≤ c. f(n)
for some constant c<1, the complexity is T(n)=Θ(f(n)).
 In Quick Sort:

 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.

Thus, the recurrence relation for Quick Sort becomes:


T(n)=2⋅T(n/2)+O(n)
 Applying Master therom:

For Quick Sort:

a=2, b=2 , f(n)=O(n) So, 𝑛log𝑏 𝑎 =𝑛log2 2 = 𝑛


Here, f(n)= Θ ( 𝑛log𝑏 𝑎 )so we are in the second case of the Master
Theorem. According to this case, T(n)=Θ(n log n)

So, the average-case time complexity of Quick Sort is O(n log n)

Worst-Case Time Complexity

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)

So, the worst-case time complexity of Quick Sort is O(𝑛2 ).

Space Complexity:

 Auxiliary Space: Quick Sort is often implemented in-place, so it


does not require additional space for arrays. However, it uses the
call stack for recursive calls.
 Average Space Complexity: The depth of recursion in the average
case is O(log n), so the space complexity is O(log n)
 Worst-Case Space Complexity: In the worst case (highly
unbalanced partitions), the recursion depth can go up to O(n),
resulting in O(n) space complexity for the call stack.

Summary

 Average-Case Time Complexity: O(n log n)


 Worst-Case Time Complexity: O(𝑛2 )
 Average-Case Space Complexity: O(log n)
 Worst-Case Space Complexity: O(n))

You might also like