Reputation: 372
I stumbled on some issue in my codes, supposedly a return will stop my method but in my case isn't. I tried to make a method called "binarySearch" which supposedly do what its made to do.
public int binarySearch(int lowIndex, int highIndex, int[] arr, int val) {
int middleIndex = (lowIndex + highIndex ) / 2;
if(arr[middleIndex] < val) {
lowIndex = middleIndex;
} else if (arr[middleIndex] > val) {
highIndex = middleIndex;
} else {
return middleIndex;
}
binarySearch(lowIndex, highIndex, arr, val);
return 0;
}
the problem is if I already found the index where the search value reside else statement will return it and stop already. but instead, I always get "0", which is I think the value i set for default return return 0
. so for some clarification, I added some text on my else statement to make sure it executed and return middleIndex
, then the text showed up so basically my loop enters else statement and hopefully returned middleIndex
but its not. maybe recursion has to do with this but I don't know maybe you guys can help me.
Upvotes: 0
Views: 910
Reputation: 156
There are minor mistakes in your code.
In recursion, when a function recursively calls itself, the function call should be placed in a return statement. This makes sure that the result of the recursive function calls is returned to calling function.
Here is the modified code -
class BinarySearch
{
// Returns index of x if it is present in arr[l..
// r], else return 0
public int binarySearch(int lowIndex, int highIndex, int[] arr, int val)
{
if(highIndex>=lowIndex) //this line should be added for safety
{
int middleIndex = (lowIndex + highIndex ) / 2;
if(arr[middleIndex] < val)
{
lowIndex = middleIndex;
}
else if (arr[middleIndex] > val)
{
highIndex = middleIndex;
}
else
{
return middleIndex;
}
return binarySearch(lowIndex, highIndex, arr, val); //this should be the return statement
}
return 0;
}
// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = {1,2,3,4,5,6,7,8};
int n = arr.length;
int x = 7;
int result = ob.binarySearch(0,n-1,arr,x);
if (result == 0)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
}
This code works fine.
Recursive stack explanation for the above code-
binarySearch(0,7,arr,val)
= return (binarySearch(3,7,arr,val))
= return (return (binarySearch(5,7,arr,val)))
= return (return (return 6)) //here 6 is the middleValue that is returned in else block
= return (return 6)
= return 6
Since, you didnt add the return statement in your code, your topmost function call in the recursive stack was returning 0.
Upvotes: 0
Reputation:
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
you can do it by recursion also.
Upvotes: 0
Reputation:
Here is iterative java code for Binary Search, yours is somewhat flawed.
int binarySearch(int arr[], int x) {
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r-l)/2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
Upvotes: 0
Reputation: 8318
Since the signature of your method says public int binarySearch
, that means you should return the int from the recursive call of binarySearch
method. You shouldn't really return 0
in a proper implementation of this method.
Upvotes: 1
Reputation: 11917
You have to return the result of your recursion. You can refer to this code shown below to understand what you are doing wrong.
// Returns index of x if it is present in arr[l..
// r], else return -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r>=l)
{
int mid = l + (r - l)/2;
// If the element is present at the
// middle itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid+1, r, x);
}
// We reach here when element is not present
// in array
return -1;
}
For more information regarding BinarySearch and its implementation you can follow this link
Upvotes: 0