| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice Test
Share
medium Solved by 6 students
Time Complexity of Binary Search
< Prev
Next >

Determine the time complexity (Big-O notation) of the following algorithm.

public int binarySearch(int[] sorted, int target) {
    int low = 0, high = sorted.length - 1;
    while(low <= high) {
        int mid = (low + high) / 2;
        if(sorted[mid] == target) return mid;
        else if(sorted[mid] < target) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}
A

O(log n)

B

O(n)

C

O(1)

D

O(n log n)

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1bommasam00025 1m 39s 71
#2suhanakochhar00612 1m 05s 25
#3geethasailaja12 1m 41s -11
#4y.seong202711 2m 46s -66
#5singhris00012 7m 40s -370
APFIVE © 2020.
Email: [email protected]|Privacy Policy