Welcome to Westonci.ca, your go-to destination for finding answers to all your questions. Join our expert community today! Connect with a community of experts ready to help you find accurate solutions to your questions quickly and efficiently. Get quick and reliable solutions to your questions from a community of experienced experts on our platform.

Trace through recursiveBinarySearch(sortedArray, 22, 0, 8) looking for the target number 22 where sortedArray = {2, 5, 8, 10, 11, 15, 17, 20, 22}. Write down each middle element that is checked and the start and end index for each recursive call. How many elements did the binary search have to check before finding 22? How would this compare to a linear search?

Sagot :

The exercise requires you to run a trace using Recursion in C script Program. This is also known as Binary Search. See details below on how to do this.

How do you run a Trace using Recursion Binary Search in C program?

Binary Search refers to an algorithm that allows you to perform a search locate the position of an element or target value in an array that is sorted.

See the algorith below:

#include <stdio.h>

int recursiveBinarySearch(int array[], int start_index, int end_index, int element){

  if (end_index >= start_index){

     int middle = start_index + (end_index - start_index )/2;

     if (array[middle] == element)

        return middle;

     if (array[middle] > element)

        return recursiveBinarySearch(array, start_index, middle-1, element);

     return recursiveBinarySearch(array, middle+1, end_index, element);

  }

  return -1;

}

int main(void){

  int array[] = {2, 5, 8, 10, 11, 15, 17, 20, 22};

  int n = 22;

  int element = 9;

  int found_index = recursiveBinarySearch(array, 0, n-1, element);

  if(found_index == -1 ) {

     printf("Element not found in the array ");

  }

  else {

     printf("Element found at index : %d",found_index);

  }

  return 0;

Learn more about Binary Search at:
https://brainly.com/question/15402776
#SPJ1