Exercises · Linear and binary search¶
Exercises · Chapter 4 · each solution opens with a click · Chapter 4: Linear and binary search · Exercises, volume 1 (PDF)
In the exercises of this chapter the searches are carried out with the two algorithms of the chapter «Linear and binary search», recalled below. The indices of the arrays go from \(1\) to \(n\), and the value \(-1\) is returned when the target value \(m\) is not in the array.
Algorithm: LinearSearch
Algorithm: BinarySearch
Exercise 4.1: Linear and binary search (E1)
Consider the following array:
with \(n = 8\).
Questions
-
Apply the Linear Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 11\)
-
\(m = 7\)
-
\(m = 9\)
-
\(m = 4\)
-
\(m = 14\)
For each search, show the execution step by step, indicating at each iteration the values of \(i\), \(a[i]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 11\):
iteration \(i\) \(a[i]\) action \(1\) 1 7 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 2 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 14 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 8 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 11 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 11 is found at index 6.
-
Search for \(m = 7\):
iteration \(i\) \(a[i]\) action \(1\) 1 7 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 7 is found at index 1.
-
Search for \(m = 9\):
iteration \(i\) \(a[i]\) action \(1\) 1 7 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 2 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 14 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 8 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 11 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 5 \(a[i] \neq m\) \(\Rightarrow\) continue \(8\) 8 9 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 9 is found at index 8.
-
Search for \(m = 4\):
iteration \(i\) \(a[i]\) action \(1\) 1 7 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 2 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 14 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 8 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 11 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 5 \(a[i] \neq m\) \(\Rightarrow\) continue \(8\) 8 9 \(a[i] \neq m\) \(\Rightarrow\) continue \(-\) 9 \(-\) \(i > n\) \(\Rightarrow\) loop ends, Not found! Accordingly, element 4 is not in the array (return \(-1\)).
-
Search for \(m = 14\):
iteration \(i\) \(a[i]\) action \(1\) 1 7 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 2 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 14 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 14 is found at index 3.
Exercise 4.2: Linear and binary search (E2)
Consider the following array:
with \(n = 7\).
Questions
-
Apply the Linear Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 22\)
-
\(m = 15\)
-
\(m = 17\)
-
\(m = 10\)
-
\(m = 1\)
For each search, show the execution step by step, indicating at each iteration the values of \(i\), \(a[i]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 22\):
iteration \(i\) \(a[i]\) action \(1\) 1 15 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 4 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 22 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 22 is found at index 3.
-
Search for \(m = 15\):
iteration \(i\) \(a[i]\) action \(1\) 1 15 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 15 is found at index 1.
-
Search for \(m = 17\):
iteration \(i\) \(a[i]\) action \(1\) 1 15 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 4 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 22 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 8 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 1 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 30 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 17 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 17 is found at index 7.
-
Search for \(m = 10\):
iteration \(i\) \(a[i]\) action \(1\) 1 15 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 4 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 22 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 8 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 1 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 30 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 17 \(a[i] \neq m\) \(\Rightarrow\) continue \(-\) 8 \(-\) \(i > n\) \(\Rightarrow\) loop ends, Not found! Accordingly, element 10 is not in the array (return \(-1\)).
-
Search for \(m = 1\):
iteration \(i\) \(a[i]\) action \(1\) 1 15 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 4 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 22 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 8 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 1 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 1 is found at index 5.
Exercise 4.3: Linear and binary search (E3)
Consider the following array:
with \(n = 7\).
Questions
-
Apply the Linear Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 27\)
-
\(m = 6\)
-
\(m = 33\)
-
\(m = 15\)
-
\(m = 3\)
For each search, show the execution step by step, indicating at each iteration the values of \(i\), \(a[i]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 27\):
iteration \(i\) \(a[i]\) action \(1\) 1 6 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 19 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 27 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 27 is found at index 4.
-
Search for \(m = 6\):
iteration \(i\) \(a[i]\) action \(1\) 1 6 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 6 is found at index 1.
-
Search for \(m = 33\):
iteration \(i\) \(a[i]\) action \(1\) 1 6 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 19 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 27 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 12 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 5 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 33 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 33 is found at index 7.
-
Search for \(m = 15\):
iteration \(i\) \(a[i]\) action \(1\) 1 6 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 19 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 27 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 12 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 5 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 33 \(a[i] \neq m\) \(\Rightarrow\) continue \(-\) 8 \(-\) \(i > n\) \(\Rightarrow\) loop ends, Not found! Accordingly, element 15 is not in the array (return \(-1\)).
-
Search for \(m = 3\):
iteration \(i\) \(a[i]\) action \(1\) 1 6 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 19 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 3 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 3 is found at index 3.
Exercise 4.4: Linear and binary search (E4)
Consider the following array:
with \(n = 7\).
Questions
-
Apply the Linear Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 10\)
-
\(m = 50\)
-
\(m = 5\)
-
\(m = 30\)
-
\(m = 75\)
For each search, show the execution step by step, indicating at each iteration the values of \(i\), \(a[i]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 10\):
iteration \(i\) \(a[i]\) action \(1\) 1 50 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 25 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 75 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 10 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 10 is found at index 4.
-
Search for \(m = 50\):
iteration \(i\) \(a[i]\) action \(1\) 1 50 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 50 is found at index 1.
-
Search for \(m = 5\):
iteration \(i\) \(a[i]\) action \(1\) 1 50 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 25 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 75 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 10 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 40 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 60 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 5 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 5 is found at index 7.
-
Search for \(m = 30\):
iteration \(i\) \(a[i]\) action \(1\) 1 50 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 25 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 75 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 10 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 40 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 60 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 5 \(a[i] \neq m\) \(\Rightarrow\) continue \(-\) 8 \(-\) \(i > n\) \(\Rightarrow\) loop ends, Not found! Accordingly, element 30 is not in the array (return \(-1\)).
-
Search for \(m = 75\):
iteration \(i\) \(a[i]\) action \(1\) 1 50 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 25 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 75 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 75 is found at index 3.
Exercise 4.5: Linear and binary search (E5)
Consider the following array:
with \(n = 8\).
Questions
-
Apply the Linear Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 24\)
-
\(m = 11\)
-
\(m = 2\)
-
\(m = 8\)
-
\(m = 3\)
For each search, show the execution step by step, indicating at each iteration the values of \(i\), \(a[i]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 24\):
iteration \(i\) \(a[i]\) action \(1\) 1 11 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 7 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 18 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 24 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 24 is found at index 5.
-
Search for \(m = 11\):
iteration \(i\) \(a[i]\) action \(1\) 1 11 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 11 is found at index 1.
-
Search for \(m = 2\):
iteration \(i\) \(a[i]\) action \(1\) 1 11 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 7 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 18 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 24 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 9 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 16 \(a[i] \neq m\) \(\Rightarrow\) continue \(8\) 8 2 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 2 is found at index 8.
-
Search for \(m = 8\):
iteration \(i\) \(a[i]\) action \(1\) 1 11 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 7 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 3 \(a[i] \neq m\) \(\Rightarrow\) continue \(4\) 4 18 \(a[i] \neq m\) \(\Rightarrow\) continue \(5\) 5 24 \(a[i] \neq m\) \(\Rightarrow\) continue \(6\) 6 9 \(a[i] \neq m\) \(\Rightarrow\) continue \(7\) 7 16 \(a[i] \neq m\) \(\Rightarrow\) continue \(8\) 8 2 \(a[i] \neq m\) \(\Rightarrow\) continue \(-\) 9 \(-\) \(i > n\) \(\Rightarrow\) loop ends, Not found! Accordingly, element 8 is not in the array (return \(-1\)).
-
Search for \(m = 3\):
iteration \(i\) \(a[i]\) action \(1\) 1 11 \(a[i] \neq m\) \(\Rightarrow\) continue \(2\) 2 7 \(a[i] \neq m\) \(\Rightarrow\) continue \(3\) 3 3 \(a[i] = m\) \(\Rightarrow\) Found! Accordingly, element 3 is found at index 3.
Exercise 4.6: Linear and binary search (E6)
Consider the following array, sorted in non-decreasing order:
with \(n = 12\).
Questions
-
Apply the Binary Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 23\)
-
\(m = 2\)
-
\(m = 89\)
-
\(m = 50\)
-
\(m = 1\)
For each search, show the execution step by step, indicating at each iteration the values of \(left\), \(right\), \(mid\), \(a[mid]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 23\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 12 6 23 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 23 is found at index 6.
-
Search for \(m = 2\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 12 6 23 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 5\) \(2\) 1 5 3 8 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 2\) \(3\) 1 2 1 2 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 2 is found at index 1.
-
Search for \(m = 89\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 12 6 23 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 7\) \(2\) 7 12 9 56 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 10\) \(3\) 10 12 11 78 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 12\) \(4\) 12 12 12 89 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 89 is found at index 12.
-
Search for \(m = 50\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 12 6 23 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 7\) \(2\) 7 12 9 56 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 8\) \(3\) 7 8 7 38 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 8\) \(4\) 8 8 8 45 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 9\) \(-\) 9 8 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 50 is not in the array (return \(-1\)).
-
Search for \(m = 1\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 12 6 23 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 5\) \(2\) 1 5 3 8 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 2\) \(3\) 1 2 1 2 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 0\) \(-\) 1 0 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 1 is not in the array (return \(-1\)).
Exercise 4.7: Linear and binary search (E7)
Consider the following array, sorted in non-decreasing order:
with \(n = 8\).
Questions
-
Apply the Binary Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 7\)
-
\(m = 1\)
-
\(m = 15\)
-
\(m = 10\)
-
\(m = 20\)
For each search, show the execution step by step, indicating at each iteration the values of \(left\), \(right\), \(mid\), \(a[mid]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 7\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 8 4 7 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 7 is found at index 4.
-
Search for \(m = 1\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 8 4 7 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 3\) \(2\) 1 3 2 3 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 1\) \(3\) 1 1 1 1 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 1 is found at index 1.
-
Search for \(m = 15\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 8 4 7 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 5\) \(2\) 5 8 6 11 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 7\) \(3\) 7 8 7 13 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 8\) \(4\) 8 8 8 15 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 15 is found at index 8.
-
Search for \(m = 10\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 8 4 7 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 5\) \(2\) 5 8 6 11 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 5\) \(3\) 5 5 5 9 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 6\) \(-\) 6 5 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 10 is not in the array (return \(-1\)).
-
Search for \(m = 20\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 8 4 7 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 5\) \(2\) 5 8 6 11 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 7\) \(3\) 7 8 7 13 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 8\) \(4\) 8 8 8 15 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 9\) \(-\) 9 8 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 20 is not in the array (return \(-1\)).
Exercise 4.8: Linear and binary search (E8)
Consider the following array, sorted in non-decreasing order:
with \(n = 6\).
Questions
-
Apply the Binary Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 40\)
-
\(m = 10\)
-
\(m = 60\)
-
\(m = 25\)
-
\(m = 70\)
For each search, show the execution step by step, indicating at each iteration the values of \(left\), \(right\), \(mid\), \(a[mid]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 40\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 30 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 4\) \(2\) 4 6 5 50 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 4\) \(3\) 4 4 4 40 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 40 is found at index 4.
-
Search for \(m = 10\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 30 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 2\) \(2\) 1 2 1 10 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 10 is found at index 1.
-
Search for \(m = 60\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 30 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 4\) \(2\) 4 6 5 50 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 6\) \(3\) 6 6 6 60 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 60 is found at index 6.
-
Search for \(m = 25\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 30 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 2\) \(2\) 1 2 1 10 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 2\) \(3\) 2 2 2 20 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 3\) \(-\) 3 2 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 25 is not in the array (return \(-1\)).
-
Search for \(m = 70\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 30 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 4\) \(2\) 4 6 5 50 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 6\) \(3\) 6 6 6 60 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 7\) \(-\) 7 6 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 70 is not in the array (return \(-1\)).
Exercise 4.9: Linear and binary search (E9)
Consider the following array, sorted in non-decreasing order:
with \(n = 6\).
Questions
-
Apply the Binary Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 15\)
-
\(m = 4\)
-
\(m = 42\)
-
\(m = 20\)
-
\(m = 2\)
For each search, show the execution step by step, indicating at each iteration the values of \(left\), \(right\), \(mid\), \(a[mid]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 15\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 15 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 15 is found at index 3.
-
Search for \(m = 4\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 15 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 2\) \(2\) 1 2 1 4 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 4 is found at index 1.
-
Search for \(m = 42\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 15 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 4\) \(2\) 4 6 5 23 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 6\) \(3\) 6 6 6 42 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 42 is found at index 6.
-
Search for \(m = 20\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 15 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 4\) \(2\) 4 6 5 23 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 4\) \(3\) 4 4 4 16 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 5\) \(-\) 5 4 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 20 is not in the array (return \(-1\)).
-
Search for \(m = 2\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 6 3 15 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 2\) \(2\) 1 2 1 4 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 0\) \(-\) 1 0 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 2 is not in the array (return \(-1\)).
Exercise 4.10: Linear and binary search (E10)
Consider the following array, sorted in non-decreasing order:
with \(n = 10\).
Questions
-
Apply the Binary Search algorithm to the array \(a\) for each of the following target values:
-
\(m = 18\)
-
\(m = 3\)
-
\(m = 30\)
-
\(m = 10\)
-
\(m = 35\)
For each search, show the execution step by step, indicating at each iteration the values of \(left\), \(right\), \(mid\), \(a[mid]\) and the result of the comparison, and give the value returned by the algorithm.
-
Solution
-
Search for \(m = 18\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 10 5 15 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 6\) \(2\) 6 10 8 24 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 7\) \(3\) 6 7 6 18 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 18 is found at index 6.
-
Search for \(m = 3\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 10 5 15 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 4\) \(2\) 1 4 2 6 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 1\) \(3\) 1 1 1 3 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 3 is found at index 1.
-
Search for \(m = 30\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 10 5 15 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 6\) \(2\) 6 10 8 24 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 9\) \(3\) 9 10 9 27 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 10\) \(4\) 10 10 10 30 \(a[mid] = m\) \(\Rightarrow\) Found! Accordingly, element 30 is found at index 10.
-
Search for \(m = 10\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 10 5 15 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 4\) \(2\) 1 4 2 6 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 3\) \(3\) 3 4 3 9 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 4\) \(4\) 4 4 4 12 \(a[mid] > m\) \(\Rightarrow\) \(right \leftarrow 3\) \(-\) 4 3 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 10 is not in the array (return \(-1\)).
-
Search for \(m = 35\):
iteration \(left\) \(right\) \(mid\) \(a[mid]\) action \(1\) 1 10 5 15 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 6\) \(2\) 6 10 8 24 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 9\) \(3\) 9 10 9 27 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 10\) \(4\) 10 10 10 30 \(a[mid] < m\) \(\Rightarrow\) \(left \leftarrow 11\) \(-\) 11 10 \(-\) \(-\) \(left > right\) \(\Rightarrow\) Not found! Accordingly, element 35 is not in the array (return \(-1\)).