Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
Exercise 3 MA407
section of the list where the entry is searched is halved in each iteration.
More precisely, suppose you have a sorted list x[0],...,x[n❂1] of integers in non-decreasing order. We call these integers keys and allow repetitions in the list. When searching for an entry with value key in x, binary search first looks at the middle element x[⌊ n 2 ⌋] and compares it to the value key. If key < x[⌊ n 2 ⌋], then we can forget about the upper half of the list and continue searching for key in the lower half x[0],...,x[⌊ n 2 ⌋ − 1]. Else, we search in the upper half x[⌊ n 2 ⌋],...,x[n❂1]. Assume the former happens (the case that the later happens is analogous). Then, we proceed similarly and compare key to the middle element x[⌊ n 4 ⌋] of this part of the list, and again, either continue with the upper half or the lower half and so on.
Exercise 3.1.
Write your answer as a comment in the file BinarySearch.py that you are asked to create below.
The applications of binary search, obviously, are manifold, and, naturally, they are not re stricted to situations where we want to find numbers. As long as the data we are working with can be ordered, we can apply this concept to find some particular data. For example, if the contacts on your phone are ordered by last name, finding an entry with a particular last name can be done with the help of binary search. (This is not, however, what actually happens on the phone: Rather, the data is stored in more complex data structures – a topic that we shall come back to.)
A small detour:
So we only needed 3 rounds of testing, and performed only 3 · 100 tests in total to pinpoint all positive students – which clearly is a cost- and time-saving mechanism. (In real life, of course, batch testing is more complicated, and there are many practical obstacles to consider; but the basic idea is similar.)
I hope this serves as sufficient motivation to look at binary search more in detail. We will, from now on, return to the simple scenario of finding a number in an ordered list of integers.
The remainder of this exercise set asks you to implement binary search and some test cases.
For this purpose, you should create a Python file BinarySearch.py containing the functions described below. Please include in this file your answer to Exercise 3.1 as a comment.
Exercise 3.2.
Write a recursive function binsearch() which, when called with binsearch(key,x,i,j), searches for the integer key among the list entries x[i],...,x using the binary search strategy described above. The entries of x are assumed to be in sorted (non decreasing) order but can have repetitions. The return value of this function should be an integer k that is the smallest list index with the property x[k]==key, if there is any such index k. If not (so, key is not among the entries x[i],...,x) then the return value should be the index k so that the assignment x[k]=key would still keep the list sorted (if the list elements x[k],...,x were shifted up to x[k+1],...,x[j]).
Here is an example: Suppose the list x of integers contains as entries the numbers x[0]=1, x[1]=4, x[2]=4, x[3]=4, x[4]=8, x[5]=9. Then binsearch(8,x,0,6) should return (the list index) 4 since x[4]=8. Similarly, binsearch(8,x,3,5) should return 4. Moreover, binsearch(4,x,0,6) should return 1 since x[1]=4, and since 1 is the first index i so that x[i]=4 (of course this is an arbitrary rule, but here we ask for this particular behaviour). binsearch(7,x,0,6) should return 4 since the “correct” place for key 7 is at index 4, because in x[4] we find the first value in the list that is bigger than 7.
Test your binsearch() function with a small example in a separate test() function in your file (see also Exercise 3.3 below). Additionally, we want to include some test cases in the test() function.
For each test, you should print information on whether the key was found, and where.
(See the example below.)The output of your program should look similar to the following.