Please answer Question # 6 Write a program that generates a table which compares
ID: 3639371 • Letter: P
Question
Please answer Question # 6
Write a program that generates a table which compares the performance of two algorithms-linear and binary search-when used to find a randomly chosen integer key in a sorted integer array. The linear search algorithm simply goes through each clement of the array in turn until it finds the desired one or determines that the key docs not appear. The binary search algorithm, which is implemented for string arrays in Figure 4-5, uses a divide-and-conquer strategy by checking the middle element of the array and then deciding which of the two remaining subarrays to search. The table you generate in this problem, rather than computing the time as in exercise 3, should instead calculate the number of comparisons made against elements of the array. To ensure that the results are not completely random, your program should average the results over several independent trials. A sample run of the program might look like this:Explanation / Answer
Linear Search Algorithm: int count=0; linear_search(Array[0....N-1], value) for(i=0....N-1) if(Array[i]== value) count ++; return i; break else continue; Binary Search's algorithm: int count = 0; initialize low to Array[0] and high as Array[N-1] binary_search(Array[0..N-1], value, low, high): if (high value): count++; return binary_search(A, value, low, mid-1) else if (A[mid]Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.