Binary search Consider the binary search algorithm implementation below. def binarySearch [T] (arr: Array [T], e: T) (using ord: Ordering [T]) = def inner (lo: Int, hi: Int): Boolean = if lohi then val mid= lo + ((hi-lo) / 2) val cmp ord.compare(e, arr(mid)) if cmp=0 then true // e == arr (mid) else if cmp < 0 then inner (lo, mid-1) // e< arr(mid) else inner(mid+1, hi) //e> arr(mid) else false inner (0, arr.length-1) Simulate its execution on the sorted array below when searching for the value -8. Click on the array elements in the same order the algorithm accesses them. Note: submission, reloading the page, and a session timeout will create a new instance! 3 4 9 10 11 12 13 14 15

icon
Related questions
Question
Binary search
Consider the binary search algorithm implementation below.
def binarySearch[T](arr: Array[T], e: T) (using ord: Ordering[T]) =
def inner (lo: Int, hi: Int): Boolean
if lo <= hi then
val mid =
lo + ((hi lo) / 2)
val cmp = ord.compare(e, arr(mid))
if cmp ==0 then true
=
// e
arr(mid)
else if cmp < 0 then inner(lo, mid-1) // e ‹ arr(mid)
else inner(mid+1, hi)
// e › arr(mid)
else
false
inner(0, arr.length-1)
Accessed indices:
==
Simulate its execution on the sorted array below when searching for the value -8.
Click on the array elements in the same order the algorithm accesses them.
Note: submission, reloading the page, and a session timeout will create a new instance!
o
3 4 5 6 7 19 10 11 12 13 14 15
Transcribed Image Text:Binary search Consider the binary search algorithm implementation below. def binarySearch[T](arr: Array[T], e: T) (using ord: Ordering[T]) = def inner (lo: Int, hi: Int): Boolean if lo <= hi then val mid = lo + ((hi lo) / 2) val cmp = ord.compare(e, arr(mid)) if cmp ==0 then true = // e arr(mid) else if cmp < 0 then inner(lo, mid-1) // e ‹ arr(mid) else inner(mid+1, hi) // e › arr(mid) else false inner(0, arr.length-1) Accessed indices: == Simulate its execution on the sorted array below when searching for the value -8. Click on the array elements in the same order the algorithm accesses them. Note: submission, reloading the page, and a session timeout will create a new instance! o 3 4 5 6 7 19 10 11 12 13 14 15
Expert Solution
steps

Step by step

Solved in 6 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Hello,its incorrect

Your task was to simulate the given binary search algorithm by listing the accessed indices on the following input.
• The input array:
-8,-6,-5,-2,-2,-1,1,2,6,9,11,12,20,25,31,32
• The value searched for: 33
Your solution: 7,3,1,0
Error: the choise number 2 to access the index 3 is wrong, it should have accessed the index 11
Transcribed Image Text:Your task was to simulate the given binary search algorithm by listing the accessed indices on the following input. • The input array: -8,-6,-5,-2,-2,-1,1,2,6,9,11,12,20,25,31,32 • The value searched for: 33 Your solution: 7,3,1,0 Error: the choise number 2 to access the index 3 is wrong, it should have accessed the index 11
Solution
Bartleby Expert
SEE SOLUTION