GATE / CS ARCHIVE

GATE CSE 2024 SET 1 / QUESTION 17

Upper and lower bounds for a single pass

Algorithms · Searching, Sorting & Selection · Full GATE CSE 2024 Set 1 paper

GATE 2024 Set 1, Q17 1 mark · MCQ
Report issue

Searching, Sorting & Selection

Question 17, GATE 2024: An algorithm checks whether an N-element integer array is sorted in either ascending or descending order by one pass comparing adjacent elements. Its worst-case running time is: A both O(N) and Ω(N); B O(N) but not Ω(N); C Ω(N) but not O(N); D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: Big-O and big-Omega describe upper and lower bounds on the same worst-case running-time function; they are not synonyms for worst case and best case.
1. Maintain whether any adjacent pair increases and whether any adjacent pair decreases. Each of the N−1 adjacent pairs requires only constant work. The single pass therefore takes at most cN+d operations, giving O(N).
2. On an already sorted array, no early contradiction occurs. The algorithm must examine through the final adjacent pair to certify the property. Such an input requires a number of comparisons proportional to N, giving a worst-case Ω(N) lower bound.
3. Together these bounds give Θ(N).
Answer: A. Some unsorted inputs may allow early termination, but that does not remove the linear worst-case lower bound.

Original paper, page 11 · Answer key