What is the worst case running time of quick sort algorithm?
Quicksort is a popular sorting algorithm known for its efficiency in practice. However, its performance varies significantly depending on the choice of the pivot element.
Quicksort works by:
The base case of the recursion is arrays of size zero or one, which are already sorted.
The time complexity of Quicksort depends on how balanced the partitioning is. Let n be the number of elements in the array (corresponding to 'x' in the options).
The worst-case scenario happens when the pivot element is always the smallest or the largest element in the current sub-array. This results in one partition with n-1 elements and the other with 0 elements.
Let's look at the recurrence relation for the worst case:
$T(n) = T(n-1) + T(0) + \Theta(n)$
Here:
Since $T(0)$ is negligible for large n, the recurrence simplifies to:
$T(n) = T(n-1) + \Theta(n)$
We can expand this recurrence relation:
Substituting back:
$T(n) = (T(n-2) + c(n-1)) + cn = T(n-2) + c(n-1) + cn$
$T(n) = (T(n-3) + c(n-2)) + c(n-1) + cn = T(n-3) + c(n-2) + c(n-1) + cn$
Continuing this pattern down to $T(1)$ (which is $\Theta(1)$):
$T(n) = T(1) + c(2) + c(3) + \dots + c(n-1) + cn$
$T(n) = \Theta(1) + c \sum_{i=2}^{n} i$
The sum $\sum_{i=2}^{n} i$ is approximately $\sum_{i=1}^{n} i - 1 = \frac{n(n+1)}{2} - 1$, which is $\Theta(n^2)$.
Therefore, the worst-case time complexity is $\Theta(n^2)$.
Comparing this with the given options, where 'x' represents 'n':
| Option | Expression (with n) | Complexity |
|---|---|---|
| 1 | $\Theta(n \log_2 n^2)$ | $\Theta(n \cdot 2 \log n) = \Theta(n \log n)$ |
| 2 | $\Theta(n)$ | $\Theta(n)$ |
| 3 | $\Theta(n \log^2 n)$ | $\Theta(n (\log n)^2)$ |
| 4 | $\Theta(n^2)$ | $\Theta(n^2)$ |
The worst case running time of quicksort algorithm is $\Theta(n^2)$.
| Case | Recurrence Relation | Time Complexity |
|---|---|---|
| Best Case | $T(n) = 2T(n/2) + \Theta(n)$ | $\Theta(n \log n)$ |
| Average Case | $T(n) \approx 2T(n/2) + \Theta(n)$ | $\Theta(n \log n)$ |
| Worst Case | $T(n) = T(n-1) + \Theta(n)$ | $\Theta(n^2)$ |
While the worst-case complexity of Quicksort is $\Theta(n^2)$, it is rarely encountered in practice with good pivot selection strategies.
Despite its potential worst case, Quicksort is often faster than other $\Theta(n \log n)$ algorithms like Merge Sort for many practical inputs due to its smaller constant factors and cache efficiency.
Which of the following is stable sort
Arrange the following items in ascending order using Bubble Sort. What is the intermediate sequence of 37, 54, 21, 85, 68, 12, 9, and 57 after the second pass?
Which of the following algorithm design approach is used in Quick sort algorithm?
Sort the following list using the Radix sort algorithm.
329, 457, 839, 436, 720, 355, 657
What is the output of the algorithm after the second pass?
You are given a sequence of n elements to sort. The input sequence consists of n/k subsequences, each containing k elements. The elements in a given subsequence are all smaller than the elements in the succeeding subsequence and larger than the elements in the preceding subsequence. Thus, all that is needed to sort the whole sequence of length n is to sort the k elements in each of the n/k subsequences.
The lower bound on the number of comparisons needed to solve this variant of the sorting problem is :