Quick Sort - Time Complexity
Time Complexity of Quick Sort
Algorithm:
QuickSort(left,
right){
If(left < right){
Pi= partition(left,right)
QuickSort(left,pi)
QuickSort(pi+1, right)
}
}
The algorithm has Recursion. But the Time complexity depends on the "Partition()" function of the Algorithm.
Assuming partition is done with the middle of the array.........
Total number of Executions = n log (n)
Time Complexity = F(n) = O (n log (n ) ) ------------- (Best Case)
Note that this time complexity calculation is for the Best Case. That means partitioning is done by the middle of the array. But this is not practical. Because every array will not partition from the middle. That means achieving the best case is not possible.
So we have to consider the Worst Case
The worst-case means partitioning is done from the end of the array. partitioning will start from the starting or ending point and it will perform until it gets one element in the array. The following diagram shows an Example.
Total Number of Executions= 1+2+....+ n = n (n+1)
2
Worst case Time Complexity = F(n) = O (n2)
F(n) = O (n2) ---- (Worst Case)
Comments
Post a Comment