Last active
June 21, 2020 22:21
-
-
Save bhaveshmunot1/5f6a9f0df5c8cf6237ca0e230787027f to your computer and use it in GitHub Desktop.
Find time complexity of binary search.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int binary_search(vector<int> a, int low, int high, int key) { // T(n) | |
if (low > high) { // 1 | |
return -1; // 1 | |
} | |
int mid = (low+high)/2; // 1 | |
if (a[mid] == key) { // 1 | |
return mid; // 1 | |
} | |
if (a[mid] < key) { // 1 | |
return binary_search(a, mid+1, high, key); // T(n/2) | |
} else { // 1 | |
return binary_search(a, low, mid-1, key); // T(n/2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment