Skip to content

Commit

Permalink
binary search impl
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Sep 24, 2024
1 parent 045344b commit 297156a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/interview/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package interview;

public class BinarySearch {

public int binarySearch(int arr[], int x) {

int low = 0;
int high = arr.length -1;

while (low <= high) {

int mid = low + (high - low) / 2;

if (x == arr[mid])
return x;

if (x < arr[mid])
high = mid -1;
else
low = mid +1;
}

return -1;
}

}

0 comments on commit 297156a

Please sign in to comment.