Created
December 4, 2018 16:53
-
-
Save leftrk/89b3bb2164bd03e4853d0b1b4084fcdf to your computer and use it in GitHub Desktop.
二分查找
This file contains hidden or 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
| template <typename Comparable> | |
| int binarySearch(const vector<Comparable>& a, const Comparable& x) { | |
| int low = 0, high = a.size() - 1; | |
| while (low <= high) { | |
| int mid = (low + high) / 2; | |
| if (a[mid] < x) | |
| low = mid + 1; | |
| else(a[mid] > x) | |
| high = mid - 1; | |
| else | |
| return mid; | |
| } | |
| return NOT_FOUND; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment