32.1k views
4 votes
You are doing a Binary Search of an array for a value. You have compared the value with the item at the middle of the array. You find that the value you are looking for is less than the item at the middle. What is the next step?

User Jacobkim
by
3.9k points

1 Answer

3 votes

Answer:

The next step is to find the middle element in the sorted list by taking the left sublist of the middle element

Step-by-step explanation:

In order to understand the answer to the question properly; take note of the following steps;

Take for example;

Search the location of value 23 using binary search in the following:

23, 22, 30, 27, 31, 19, 35, 32, 42, 44.

Step 1: Sort the values;

19, 22, 23, 27, 30, 31, 32, 35, 42, 44

Step 2: Find the middle element in the sorted list.

This is done using the following formula low + ½(low + high)

where low = lowest index = 0

high = highest index = 9

mid = low + ½(0 + 9)

mid = 0 + ½(9)

mid = 0 + 4.5

mid = 4.5

mid = 4 (when we have a decimal point, we make use of only the integer part)

So, the midpoint is item on the 4th index; that's 30

Step 3: Compare the search element with the middle element in the sorted list

Step 4: If both are matched, then display "Given element is found!!!" and terminate the function.

Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element. Is this true? No

Step 6 - If the search element is smaller than middle element, repeat steps 2 for the left sublist of the middle element. Is this true? Yes

...........

This is the part I need (so, I'll stop here).

Step 6 says repeat step 2 but by taking the left sublist of the middle element.

This means that; we'll chunk off every items at the right hand side of the midpoint; the midpoint then becomes the high; this gives us:

19, 22, 23, 27,30

..... This is the required next step

User ChosenJuan
by
3.9k points