Please check the bug in this one
Hey all,
These are my two search algorithms: recursive binary and recursive sequential search.
For some reason I am not getting the right output
Code:
//'listSize' is a parameter that inputs size of the array into function
//'item' is a parameter that inputs the current value of the function
//list is a pointer to the array
int sequentialSearch(int item, int *list, int listSize)
{
if (listSize == 0)
return -1;
if (*list == item)
return listSize - 2;
else
list++;
return sequentialSearch(item, list, listSize - 1);
}
int binarySearch(int item, int *list, int listSize) {
if (listSize == 0) {
return -1;
}
else{
int mid = (listSize) / 2; // compute mid point.
list = list + mid;
if (item == *list)
return *list; // found it.
else if (item < *list)
// Call ourself for the lower part of the array
return binarySearch(item, list, listSize - 1);
else
// Call ourself for the upper part of the array
return binarySearch(item, list, listSize + 1);
}
}
This program should gimme the position of a number(item) in the array that I pass(*list). It also must use pointers and recursion to get credit. I am baffled as well as stressed. Please tell me if I am missing something. Thanks again guys.