Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose we are performing a binary search on a sorted array called numbers initi

ID: 3725266 • Letter: S

Question

Suppose we are performing a binary search on a sorted array called numbers initialized as follows: // index int [ ] numbers = {-30, -9, -6, -4, -2, -1, e, 2, 4, ie, 12, 17, 22, 30); /I search for the value -5 int index = binarySearch(numbers, -5); Write the indexes of the elements that would be examined by the binary search (the mid values in our algorithm's code) and write the value that would be returned from the search. Assume that we are using the binary search algorithm shown in lecture and section e 1 2 3 4 5 6 7 8 9 1e 11 12 13 indexes examined Sound F value returned

Explanation / Answer

In Binary Search is apply only on sorted arrray, Search in a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

Step1: Array (numbers) will divide in two equal parts

We didvide it from index 6 as numbers[6]=0

Step2: check the mid element with search element(in our case -5) i.e

0>-5 then we will work on first half because -5 should be available in first half.

-30 -9 -6 -4 -2 -1

Step3:Again repeat the step1 we find middle element numbers[2]=-6

-6<-5, now will take right half i.e -4 -2 -1 repeat step1

At last the function binarySearch(numbers,-5) return negative value(i.e. -1) as search element is not available in array means search was unsuccessfull.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote