Lab 4: Linear and Binary Search
Given a list containing a list of numbers, find a number in the list using linear search and binary search algorithms.
Pre-Lab Questions
How to calculate midpoint of a list? Which operator is most relevant?
Both
Linear search
andBinary search
expect that the input list to be always sorted. If the list is not sorted, both algorithms will not work. True or False? Explain.
Post-Lab Questions
If the list has 10,000 unsorted positive integers and the number you are searching for is between 1 and 10, which search algorithm will you choose? Binary or Linear? Why?
The
bisection
algorithm (available in the Python standard library as thebisect
module is useful for many applications). Use it to code up the Student Grading challenge available at http://j.mp/gradeCD. First read the instructions file for problem statement.
Bonus
As per the test output, there are four calls made to the binary_search_recursive
function before the result is arrived at. What change(s) must be made to the code to reduce the number of calls to three?
Related Material
Binary search Visualization - http://j.mp/binarySearch
Simply the very best - comparative, silent and quite effective all the same!
For visualizer debugging - https://goo.gl/aKE2ow
Online execution - https://goo.gl/Z6z33B
For Linear Search
https://thecodeaddict.wordpress.com/tag/sentinel-search/
https://www.hackerearth.com/practice/notes/sentinel-linear-search/
Test First Teaching Binary Search
Refer to cyberdojo-exercises repo for actual tested code and test cases
Use Slice to teach Binary Search, and use Binary Search as use-case for Slicing
The code is very simple and elegant to teach and understand
Version 2
Presenting a non-slice version of the Binary search then become as the the next logical step
Last updated