Which of the following sorting algorithm have a worst-case time complexity of O(n log n)?
A1. Bubble Sort
A2. Heap Sort
A3. Quick Sort
A4. Insertion Sort
A5. Selection Sort
A2 only
A2 and A3 only
A1, A2 and A3 only
A2 and A5 only
[ Option A ]
Must Know:
Algorithm | Best Case | Average Case | Worst Case | Space Complexity | Stable? |
Bubble Sort | O(n) (if already sorted) |
O(n2) | O(n2) | O(1) | Yes |
Insertion Sort | O(n) (if already sorted) |
O(n2) | O(n2) | O(1) | Yes |
Selection Sort | O(n) (if already sorted) |
O(n2) | O(n2) | O(1) | No |
Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
Quick Sort | O(n log n) | O(n log n) | O(n2) | O(log n) | No |
Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
What is the worst-case complexity of selection sort?
O(n log n)
O(log n)
O(n)
O(n2)
[ Option D ]
The worst-case time complexity of Selection Sort is O(n²). This is because, in every pass of the algorithm, the smallest (if sort in ascending order) or largest (if sort in descending order) element is selected from the unsorted portion of the array by scanning all remaining elements. This requires (n−1) comparisons in the first pass, (n−2) in the second pass, and so on, until only one element is left.
The total number of comparisons becomes (n−1)+(n−2)+...+1=n(n−1)/2, which simplifies to O(n²).
What is the recurrence relation of the best case in quick sort?
T(n) = 2T(n/2)+Ө(n)
T(n) = T(n-1)+ Ө(n)
T(n) = T(n/2)+ Ө(n2)
T(n) = 2T(n/2)+ Ө(n2)
[ Option A ]
A recurrence relation is an equation that expresses the running time of a problem in terms of smaller instances of the same problem. It is used in the analysis of recursive algorithms because recursion naturally breaks a large problem into smaller subproblems.
By writing and solving the recurrence relation, we can calculate the overall time complexity of the algorithm in an exact and systematic way.
In the best case of Quick Sort, each partition splits the array into two equal halves. The partitioning step itself requires scanning the entire array once, which takes Θ(n) time. After this step, two recursive calls are made on subarrays of size n/2 each, giving 2T(n/2). Therefore, the recurrence relation can be expressed as T(n) = 2T(n/2) + Θ(n). By applying the Master Theorem to this recurrence, we find that the solution is T(n) = Θ(n log n).
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.