Discrete Mathematics and Its Applications, Seventh Edition

Published by McGraw-Hill Education
ISBN 10: 0073383090
ISBN 13: 978-0-07338-309-5

Chapter 3 - Section 3.1 - Algorithms - Exercises - Page 202: 14

Answer

See the solution to check the steps both search algorithms would go through. Note that in both cases, the algorithms would return $0$ since $7$ is not in the set

Work Step by Step

The list of the numbers is $\{3, 4,5, 6, 8, 9, 11\}$ a) The linear search would work as follows: It would begin by iterating over the list inspecting each element until it reaches the target. For the first element $a_1$ which is $3$, It compares it to the target $7$; finding that it's not what it's looking for, it continues its search. The same procedure happens for every other element $\{4,5,6,8,9,11\}$ and since none of them is equal to $7$. The algorithm returns $0$. b) The binary search would work as follows. Notice that for each iteration, we have a left endpoint, a right endpoint and a mid-point. These will be referred to as left, right, and mid and will be modified after each cycle accordingly. left $:=1,$ right $:=7$, mid$:=\left \lfloor{\frac{1+7}{2}}\right \rfloor := 4$ We compare $7$ to $a_4\to7\gt6\to$ We limit our search to the right group, namely {${8, 9, 11}.$} We update left to mid $+ 1 := 5$. Now, our new set of indices are: left $:=5,$ right $:=7$, mid$:=\left \lfloor{\frac{5+7}{2}}\right \rfloor := 6$ We compare $7$ to $a_6$ and since $7\not\gt9$, we limit our search to the left subgroup of the set, namely ${8, 9}$. Our new set of indices are: left $:=5,$ right $:=6$, mid$:=\left \lfloor{\frac{5+6}{2}}\right \rfloor := 5$. We compare $7$ to $a_5$ and seeing as $7\not\gt8$. We limit our search to the left set, namely $\{8\}.$ Seeing as we now have only one element in the set, binary search checks if it's equal to the target (which it is not in this case) and returns $0$ .
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.