Of the following sorting algorithms, which has a running time that is least…
2018
Of the following sorting algorithms, which has a running time that is least dependent on the initial ordering of the input?
- A.
Merge Sort
- B.
Insertion Sort
- C.
Selection Sort
- D.
Quick Sort
Attempted by 212 students.
Show answer & explanation
Correct answer: A
An algorithm's running time is dependent on the initial ordering of the input when its asymptotic complexity changes from one order of growth to another depending on how the input happens to be arranged — for example, from O(n) to O(n2). It is independent of ordering when every possible arrangement of the input — already sorted, reverse sorted, or random — keeps the same order of growth.
Algorithm | Best Case | Average Case | Worst Case |
|---|---|---|---|
Merge Sort | O(n log n) | O(n log n) | O(n log n) |
Insertion Sort | O(n) | O(n2) | O(n2) |
Selection Sort | O(n2) | O(n2) | O(n2) |
Quick Sort | O(n log n) | O(n log n) | O(n2) |
Insertion Sort needs only a single pass through an already-sorted array but must shift every earlier element past the new one on a reverse-sorted array, so its complexity swings from linear to quadratic depending on how the input starts out. Quick Sort's running time depends on how each pivot splits the remaining elements: with a naive pivot rule, an already-sorted or reverse-sorted array produces the most unbalanced possible splits, collapsing its complexity from n log n to quadratic. Both algorithms are therefore clearly dependent on the initial ordering of the input.
By this test, both Merge Sort and Selection Sort qualify as order-independent: Merge Sort always follows the same divide-and-conquer recurrence — splitting the array at its midpoint and merging the two halves — regardless of the values involved, so it stays at the same O(n log n) order for every arrangement; Selection Sort always scans the remaining unsorted elements the same way on every pass, so it stays at O(n2) for every arrangement too. Taken purely as a test of whether the order of growth ever changes, the two are tied — which is exactly the ambiguity behind this question. This is a well-known previous-year exam item (ISRO CS 2018), and its established answer — consistent across independently published solved-paper references for this exact question — is Merge Sort: the property this classic item is understood to be testing is a running time that is order-independent AND efficient (O(n log n)), the standard textbook example of a sort whose speed never suffers regardless of input arrangement. Selection Sort's invariance comes bundled with an already-slow O(n2) cost in every case, so it is not the option this item's answer key selects, even though a strictly literal reading of the question text alone does not by itself rule it out.
Per the established answer key for this exam question, Merge Sort is the intended answer — the algorithm whose running time is least dependent on the initial ordering of the input.