Question: Assignment 6A: Linear Search vs. Binary Search. In the lecture class, we have repeatedly claimed that binary search is faster than linear search on



Assignment 6A: Linear Search vs. Binary Search. In the lecture class, we have repeatedly claimed that binary


Assignment 6A: Linear Search vs. Binary Search. In the lecture class, we have repeatedly claimed that binary search is faster than linear search on a sorted array. But don't take our word for it - let's try to prove it by comparing the two! In this lab, you will create an array of size one million (1,000,000). Fill it sequentially with values (e.g. array[0] should equal 0, array[1] should equal 1, etc). Then the computer should select a random target number between 0 and array size - 1. You should then search the array for this value twice - once with a linear search algorithm, and then with a binary search algorithm. Keep track of how long each one took, using the following criteria: Linear Search: Number of loop iterations before the target number was found Binary Search: Number of midpoints chosen (a.k.a "guesses") taken before the target number was found Once both algorithms have been run, display the results and print which algorithm found the number first (or if there was a tie). Sample Output #1: Comparing Linear Search and Binary Search: Our target is 307620 Linear Search: 307621 loop(s) Binary Search: 20 guess(es) Binary Search is faster this time! Sample Output #2: Comparing Linear Search and Binary Search: Our target is 1 Linear Search: 2 loop (s) Binary Search: 19 guess(es) Linear Search is faster this time!

Step by Step Solution

3.51 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Certainly Below is a simple Python code snippet to perform the descri... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!