Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of the following is true about the time complexity of the Binary Search algorithm ?

  • A.O(n)
  • B.O(logn)
  • C.O(nlogn)
  • D.O(n^2)

Answer: B

Binary search runs in O(logn) time.

Each comparison halves the part of the sorted array still to be searched, so after k steps \(n/2^k\) elements remain, and the search ends after about \(\log_2 n\) steps.

O(n) is linear search, O(n log n) is efficient sorting, and \(O(n^2)\) is simple sorting such as bubble sort.

For example, a sorted array of a million elements needs at most about 20 comparisons.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.