Last active
September 15, 2020 02:19
-
-
Save kpol/861b8f946a65a2c5833b728c6365cb8a 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
| public static int BinarySearch(int[] arr, int k) | |
| { | |
| var minIndex = 0; | |
| var maxIndex = arr.Length - 1; | |
| while (minIndex <= maxIndex) | |
| { | |
| var mid = (minIndex + maxIndex) / 2; | |
| if (k == arr[mid]) | |
| { | |
| return mid; | |
| } | |
| if (k < arr[mid]) | |
| { | |
| maxIndex = mid - 1; | |
| } | |
| else | |
| { | |
| minIndex = mid + 1; | |
| } | |
| } | |
| return -1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment