-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Divide and conquer algorithms", exercise 1 #28
Comments
|
|
This solution seems to work but it feels way to complicated to be the right one,
|
The result is
|
It returns:
|
|
|
|
|
|
Hi all, A few suggestions and tests you should try on your code:
|
Following the non-verbal definition of the "binary search" algorithm provided by Fekete and Morr, at first, I was tempted to create a sublist, but then I discovered thet it could be more useful to change the mid value time by time (i.e. with the recursive operations), so I attached as mantra the suggestion of professor Peroni. Let's dive in the exercise.
|
|
Implement the binary search algorithm in Python – i.e. the recursive function
def binary_search(item, ordered_list, start, end)
. It takes an item to search (i.e. item), an ordered list and a starting and ending positions in the list as input. It returns the position ofitem
in the list if it is in it and None otherwise. The binary search first checks if the middle item of the list between start and end (included) is equal toitem
, and returns its position in this case. Otherwise, if the central item is less thanitem
, the algorithm continues the search in the part of the list that follows the middle item. Instead, if the central item is greater thanitem
, the algorithm continues the search in the part of the list preceding the central item. Accompany the implementation of the function with the appropriate test cases. As supporting material, Fekete and Morr released a nonverbal definition of the algorithm [Fekete, Morr, 2018a], which is useful to understand the rationale of the binary search steps.The text was updated successfully, but these errors were encountered: