GATE CS 2024 Set 1 | Question 17

MCQ · 1 mark · −0.33

Given an integer array of size \(N\), 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

  1. Option A: both \(O(N)\) and \(\Omega(N)\) (correct)
  2. Option B: \(O(N)\) but not \(\Omega(N)\)
  3. Option C: \(\Omega(N)\) but not \(O(N)\)
  4. Option D: neither \(O(N)\) nor \(\Omega(N)\)

Answer

(A)

Explanation

The algorithm walks once through the array, comparing each adjacent pair \((a_{i}, a_{i+1})\), and stops as soon as a pair breaks the order. The worst case is an array that really is sorted (or breaks the order only at the very end): all \(N-1\) pairs must then be compared before the algorithm can answer.

So the worst-case time grows linearly with \(N\). It is \(\Theta(N)\), which means it is both \(O(N)\) and \(\Omega(N)\).

B fails because no method can do better: an element that is never examined could break the order, so the time is also \(\Omega(N)\). C and D fail because a single pass is \(O(N)\).

Answer: A