Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
Marked for reviewAbout Exam
medium Solved by 7 students

Time Complexity of Binary Search

< Prev
Next >

What is the time complexity in Big-O notation for the binarySearch method shown below?

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(n)

B

O(log n)

C

O(1)

D

O(n log n)

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question
Mark for review

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1bommasam00038 2m 10s 120
#2suhanakochhar00612 1m 05s 25
#3geethasailaja12 1m 41s -11
#4y.seong202711 2m 46s -66
#5singhris00012 7m 40s -370
Items per page:
10
1 – 5 of 5
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|FAQ