Question: int main(void) { // the array we're searching int test_array[] = {3, 5, 8, 12, 25, 34, 57, 58, 59, 67, 87}; // prompt user
int main(void) { // the array we're searching int test_array[] = {3, 5, 8, 12, 25, 34, 57, 58, 59, 67, 87}; // prompt user and read number int to_search = 0; printf("Please type number to search for: "); if(1 != scanf("%d", &to_search)) { fprintf(stderr, "Error reading number. Exiting program. "); return EXIT_FAILURE; } int len = sizeof(test_array)/sizeof(*test_array);
// search for the number using binary search int result = binary_search(to_search, test_array, 0, len - 1);
// report result to console if (result == -1) { printf("Element not found using binary search "); } else { printf("Element is in position %d using binary search ", result); } // search for the number using linear search int result2 = linear_search(to_search, test_array, len); if (result2 == -1) { printf("Element not found using linear search "); } else { printf("Element is in position %d using linear search ", result2); } return EXIT_SUCCESS; }
a)
Suppose the user types 67 when this program is executed. What are the recursive calls for binary_search? binary_search(67, test_array, 0, 10) binary_search(67, test_array, _____, ______) binary_search(67, test_array, _____, ______) // keep going until recursion stops b. In the above execution, what are the successive values of mid that are assigned as the function makes recursive calls? mid = 5 mid = _____ // keep going c. Suppose the user types 18. What are the recursive calls for binary_search? binary_search(18, test_array, 0, 10) binary_search(18, test_array, _____, ______) binary_search(18, test_array, _____, ______) // keep going until recursion stops d. Assume linear_search is called on (67, test_array, 11). How many numbers in test_array are examined before the function returns? ____________
e) 5. How many recursive calls were made when using binary search to find 67? ________
f. Which function is faster, in general, for searching? binary search or linear search?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
