September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,27 +1,29 @@
|
|||
...
|
||||
//check will be the number we are looking for
|
||||
//nums will be the array we are searching through
|
||||
public static int binarySearch(int[] nums, int check){
|
||||
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 - lo) / 2);
|
||||
if(nums[guess] > check){
|
||||
hi = guess - 1;
|
||||
}else if(nums[guess] < check){
|
||||
lo = guess + 1;
|
||||
}else{
|
||||
return guess;
|
||||
}
|
||||
while (hi >= lo) {
|
||||
int guess = lo + ((hi - lo) / 2);
|
||||
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[] searchMe;
|
||||
int someNumber;
|
||||
...
|
||||
int index = binarySearch(searchMe, someNumber);
|
||||
System.out.println(someNumber + ((index == -1) ? " is not in the array" : (" is at index " + index)));
|
||||
...
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,28 @@
|
|||
public static void main(String[] args){
|
||||
int[] searchMe;
|
||||
int someNumber;
|
||||
...
|
||||
int index = binarySearch(searchMe, someNumber, 0, searchMe.length);
|
||||
System.out.println(someNumber + ((index == -1) ? " is not in the array" : (" is at index " + index)));
|
||||
...
|
||||
}
|
||||
public class BinarySearchRecursive {
|
||||
|
||||
public static int binarySearch(int[] nums, int check, int lo, int hi){
|
||||
if(hi < lo){
|
||||
return -1; //impossible index for "not found"
|
||||
public static int binarySearch(int[] haystack, int needle, int lo, int hi) {
|
||||
if (hi < lo) {
|
||||
return -1;
|
||||
}
|
||||
int guess = (hi + lo) / 2;
|
||||
if(nums[guess] > check){
|
||||
return binarySearch(nums, check, lo, guess - 1);
|
||||
}else if(nums[guess]<check){
|
||||
return binarySearch(nums, check, guess + 1, hi);
|
||||
if (haystack[guess] > needle) {
|
||||
return binarySearch(haystack, needle, lo, guess - 1);
|
||||
} else if (haystack[guess] < needle) {
|
||||
return binarySearch(haystack, needle, guess + 1, hi);
|
||||
}
|
||||
return guess;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] haystack = {1, 5, 6, 7, 8, 11};
|
||||
int needle = 5;
|
||||
|
||||
int index = binarySearch(haystack, needle, 0, haystack.length);
|
||||
|
||||
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