Question
Given an integer array of size , we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by making a single pass through the array and comparing each element of the array only with its adjacent elements. The worst-case time complexity of this algorithm is
Official answer and solution
- Official answer
- A
- From
- Official GATE 2024 answer key
Solution
Determine the number of comparisons made in a single pass when the array is sorted (the worst case for verifying sortedness).
To verify whether an array of size is sorted in a single pass of adjacent comparisons, the worst case occurs when the array is actually sorted (or when the only out-of-order pair is at the very end of the array). In this worst case, the algorithm must examine all elements (performing comparisons). A function in is both and .
Option (A)
Confusing the best-case time complexity (early exit on the first few elements) with the lower bound of the worst-case time complexity function .
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2024 Computer Science and Information Technology - Set 1 (CS1)
- Question
- 17 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.