Time Complexity Analysis Steps
•Identify the basic operation.
•Count how many times it runs (depends on input size).
•Set up a recurrence relation if recursive.
•Solve the recurrence
•Consider best, average, and worst cases.
•Present the final time complexity.
Analysis of quick sort
[Link] the Basic Operation
Determine the operation that dominates the
execution time.
For QuickSort, the key operations are:
•Choosing a pivot
•Partitioning
•Recursive calls
2. Count the Number of Basic Operations
1. Partitioning takes n times.
2. Two recursive calls are made on subarrays of size ≈
n/2
3. Form a Recurrence Relation
Create a recurrence based on how the algorithm breaks the
problem down.
T(n)=2T(n/2)+cn
•T(n) is the time to sort n elements,
•cn is the time taken to partition the array
•c is a constant.
Solve the recurrence (using Sunstituion method)
We expand T(n) by repeatedly substituting the recursive terms
First expansion: T(n)=2T(n/2)+cn (n = n/2)
Second expansion :
= 2(2T(n/4)+c(n/2))+cn
=4T(n/4)+cn+cn
=4T(n/4)+2cn
Third Expansion : 4(2T(n/8)+c(n/4))+2cn
=8T(n/8)+cn+cn+cn
= 8T(n/8)+3cn
After k expansions : T(n)=2k T(n/2k)+kcn
Solve for base case
We reach the base case when:
n/2k=1⇒2k=n⇒k=log2n
Substitute K back in T(n)=2k T(n/2k)+kcn
T(n)=2log2n⋅ T(1)+cn⋅ log2n
=n⋅ T(1)+cnlogn
Assume T(1) = c₀ (a constant),
so:T(n)=n⋅ c0+cnlogn
=O(nlogn)
• Final Result:
Time Complexity T(n)=O(nlogn)
• This confirms that in the best and average
cases, QuickSort runs in linearithmic time.