Use Binary Search, Recursion (Algorithm 2.1) to search for the integer 120 in the following list (array) of integers. Show the actions step by step. \(\begin{array}{lllllllll}12 & 34 & 37 & 45 & 57 & 82 & 99 & 120 & 134\end{array}\)

Short Answer

Expert verified
The index of the integer '120' in the provided list can be found using the recursive Binary Search Algorithm and the process shows how the search space gets reduced in each step. The '120' is found at index '7'.

Step by step solution

01

Determine the Initial Parameters

Start off by deciding the edges of the list. These are indicated by the first and last indices, which in this case are 0 and 8 respectively. So now our bounds are low=0 and high=8.
02

Determine the Midpoint

Compute the midpoint of the array by summing the indices 'low' and 'high', then dividing by 2. In other words, mid=(low+high)/2. In our case, the midpoint would be \( \frac{0 + 8}{2} = 4 \). The number at index 4 is 57.
03

Compare Midpoint with Target Value

The central element is compared to the search value. The number at the midpoint (57) is less than our target value (120). As a result, we discard all values prior to this midpoint because the binary search works on sorted arrays, and all the elements before 57 are lesser.
04

Adjust the Search Space

Because the target value is bigger than the midpoint, we can modify our ‘low’ reference point. It now becomes one index greater than the midpoint, which now makes it low = mid+1 = 5. We retain the ‘high’ value as 8. Therefore, our search space is now constrained between indices 5 and 8.
05

Repeat the Procedure

We rewind to step 2 and perform the same process on the reduced list by finding the midpoint, comparing it with the target value, and subsequently adjusting the search space. Until we either find the target element or exhaust the search space.
06

Identify the Target Value

Following this approach, our next midpoint is the value at index \( \frac{5+8}{2} = 6.5 \). We round down to 6 as our new 'mid' value. However, the number at index 6 (99) is less than our target value (120). Therefore, we repeat the process and adjust our ‘low’ reference to be mid+1=7. By continuing the process, we finally find that the number at the index given by the new midpoint (7) matches with our target value (120). Hence, the search is over.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Use the divide-and-conquer approach to write a recursive algorithm that finds the maximum sum in any contiguous sublist of a given list of \(n\) real values. Analyze your algorithm, and show the results in order notation.

Write a nonrecursive algorithm for Quicksort (Algorithm 2.6). Analyze your algorithm, and give the results using order notation.

Write a divide-and-conquer algorithm for the Towers of Hanoi problem. The Towers of Hanoi problem consists of three pegs and \(n\) disks of different sizes. The object is to move the disks that are stacked, in decreasing order of their size, on one of the three pegs to a new peg using the third one as a temporary peg. The problem should be solved according to the following rules: (1) when a disk is moved, it must be placed on one of the three pegs; (2) only one disk may be moved at a time, and it must be the top disk on one of the pegs; and (3) a larger disk may never be placed on top of a smaller disk. a. Show for your algorithm that \(S(n)=2^{n}-1\). (Here \(S(n)\) denotes the number of steps (moves), given an input of \(n\) disks.) b. Prove that any other algorithm takes at least as many moves as given in part (a).

Write an algorithm that searches a sorted list of \(n\) items by dividing it into three sublists of almost \(n / 3\) items. This algorithm finds the sublist that might contain the given item and divides it into three smaller sublists of almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in the list. Analyze your algorithm and give the results using order notation.

Suppose that, even unrealistically, we are to search a list of 700 million items using Binary Search, Recursion (Algorithm 2.1). What is the maximum number of comparisons that this algorithm must perform before finding a given item or concluding that it is not in the list?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free