What is the time complexity of searching for a value in an unsorted array of n elements?
Correct answer D. O(n)
Explanation
Without any order to exploit, the only safe method is to examine elements one by one, and in the worst case the value is last or absent, requiring n comparisons. That is linear time, O(n). O(log n) needs a sorted array so binary search can discard half the range at each step; O(1) applies to index access, not to searching by value.
Answer checked How we write and check questions
Discussion