Created
September 4, 2019 03:56
-
-
Save kntmr/fc250161531390670d78d87670b90477 to your computer and use it in GitHub Desktop.
Binary search algorithm
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
package com.example.demo; | |
public class BinarySearch { | |
int binarySearch(int[] a, int n, int left, int right) { | |
if (left > right) return -1; | |
int mid = (left + right) / 2; | |
if (n < a[mid]) { | |
return binarySearch(a, n, 0, mid - 1); | |
} | |
if (n > a[mid]) { | |
return binarySearch(a, n, mid + 1, right); | |
} | |
return mid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment