Skip to content

Instantly share code, notes, and snippets.

@kntmr
Created September 4, 2019 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kntmr/fc250161531390670d78d87670b90477 to your computer and use it in GitHub Desktop.
Save kntmr/fc250161531390670d78d87670b90477 to your computer and use it in GitHub Desktop.
Binary search algorithm
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