new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
27
Task/Binary_search/Java/binary_search.java
Normal file
27
Task/Binary_search/Java/binary_search.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
...
|
||||
//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){
|
||||
int hi = nums.length - 1;
|
||||
int lo = 0;
|
||||
while(hi >= lo){
|
||||
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)));
|
||||
...
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue