Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
29
Task/Binary-search/Java/binary-search-1.java
Normal file
29
Task/Binary-search/Java/binary-search-1.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
public class BinarySearchIterative {
|
||||
|
||||
public static int binarySearch(int[] nums, int check) {
|
||||
int hi = nums.length - 1;
|
||||
int lo = 0;
|
||||
while (hi >= lo) {
|
||||
int guess = (lo + hi) >>> 1; // from OpenJDK
|
||||
if (nums[guess] > check) {
|
||||
hi = guess - 1;
|
||||
} else if (nums[guess] < check) {
|
||||
lo = guess + 1;
|
||||
} else {
|
||||
return guess;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] haystack = {1, 5, 6, 7, 8, 11};
|
||||
int needle = 5;
|
||||
int index = binarySearch(haystack, needle);
|
||||
if (index == -1) {
|
||||
System.out.println(needle + " is not in the array");
|
||||
} else {
|
||||
System.out.println(needle + " is at index " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue