Recursive binary search program help.
Im attempting to write a binary search function which calls itself recursively. I cant get it to call itself recursively w/ out freezing. The code is below, it freezes right after "calling function recursively"
I tried setting found =1 right before it calls itself, but that doesn't work meaning the program doesnt get stuck in an infinite loop, it just freezes when it calls itself.
I think it might have to do w/ allocating and freeing the memory, but am really not sure.
Thanks for the help!
Code:
#include <stdio.h>
int
BinarySearch(int *asize, int numbers[*asize], int *snum, int *searcher, int *iter, int *top, int *bottom, int *middle, int *found)
{
*middle = (int)(((*top + *bottom)/2.0)+.5);
printf("top = %d middle = %d bottom = %d\nsearcher = %d snum = %d &numbers[*middle] = %d found = %d", *top, *middle, *bottom, *searcher, *snum, numbers[*middle], *found);
if(*bottom < *top && *found == 0) {
if(searcher == numbers[*middle]) {
*found = 1;
*iter++;
printf("found %d", &numbers[*middle]);
}
else if(numbers[*middle] > *searcher) {
*top = *middle - 1;
printf("top = %d middle = %d bottom = %d\nsearcher = %d snum = %d &numbers[*middle] = %d", *top, *middle, *bottom, *searcher, *snum, numbers[*middle]);
printf("calling function recursively");
return (BinarySearch(*asize, numbers, *snum, *searcher, *iter, *top, *bottom, *middle, *found));
}
else {
*bottom = *middle + 1;
printf("top = %d middle = %d bottom = %d\nsearcher = %d snum = %d &numbers[*middle] = %d", *top, *middle, *bottom, *searcher, *snum, numbers[*middle]);
return (BinarySearch(*asize, numbers, *snum, *searcher, *iter, *top, *bottom, *middle, *found));
}
}
else{}
if(*found == 1) {
return(0);
}
else {
printf("top = %d middle = %d bottom = %d\nsearcher = %d snum = %d &numbers[*middle] = %d", *top, *middle, *bottom, *searcher, *snum, numbers[*middle]);
}
}