After the array is sorted, the program will ask user to enter a key to search for in the sorted array. It will then call bSearch to perform a Binary Search on the array. Complete the bSearch function so that it implements Binary Search recursively (no loop!) You must use pointer notations here as well. Pay attention to what is written in main so your bSearch will return an appropriate value

Respuesta :

Answer:

int bSearch(int *arr, int a, int b, int key){

if(a>b)return -1;

int mid=(a+b)/2;

if((*(arr+mid))==key)return mid;

if(key<(*(arr+mid))) return bSearch(arr,a,mid-1,key);

return bSearch(arr,mid+1,b,key);

}

int main() {

int i = 0, size = 0, key = 0, result = 0;

int *array, *sorted;

printf("How big is your array? ");

scanf("%d", &size);

array = (int*)malloc(size * sizeof(int));

for(i=0; i<size; i++){

printf("Please enter array[%d]: ", i);

scanf("%d", &array[i]);

}

result = bSearch(sorted, 0, size-1, key);

if(result != -1)

printf("Got it! A %d is at index %d. ", key, result);

else

printf("I'm sorry, a %d cannot be found in the array. ",

key);

return 0;

}

Explanation:

ACCESS MORE
EDU ACCESS