5.4 Practice Problems
Problem 1: Linear Search
Write a Python function to implement a linear search algorithm. The function should take a list and the target value as parameters and return the index of the target value if it is in the list, otherwise return -1.
Solution:
Problem 2: Binary Search
Implement a Python function for a binary search. The function should take a sorted list and the target value as parameters. If the target value is in the list, return its index. Otherwise, return -1.
Solution:
Problem 3: Hashing
Suppose you have implemented a hash table with chaining to handle collisions. Now, you are given the keys: 10, 22, 31, 4, 15, 28, 17, 88, 59. Write a Python function to build the hash table using the hash function key mod 10.
Solution:
Problem 4: Binary Search vs Linear Search
Given a list of 1000 elements, at what point (number of elements) does it become faster to use a binary search rather than a linear search? Explain your reasoning.
These problems...