Assignment 6A: Linear Search vs. Binary Search. In the lecture class, we have repeatedly claimed that...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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! 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!
Expert Answer:
Answer rating: 100% (QA)
Certainly Below is a simple Python code snippet to perform the descri... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
The Crazy Eddie fraud may appear smaller and gentler than the massive billion-dollar frauds exposed in recent times, such as Bernie Madoffs Ponzi scheme, frauds in the subprime mortgage market, the...
-
Various statistical software packages tend to provide different measures of association. Use statistical software to calculate the Goodman-Kruskal gamma, Somers D, or Kendalls tau-a for the space...
-
Early in 2013, Leon's Furniture acquired its closest competitor, The Brick. Selected financial data (in thousands) for the two companies, prior to the acquisition, are presented below. Instructions...
-
The 2018 annual report of Best Buy Co., Inc., reported profitable operations for the most recent six years. However, the company suffered a net loss in 2012. Best Buy reported the following for the...
-
What key elements should be included in a typical job description?
-
The Cora Theater, owned by Cora Graham, will begin operations in March. The Cora will be unique in that it will show only triple features of sequential theme movies. As of March 1, the ledger of Cora...
-
What is the gear ratio (rear to front) of a bicycle that has 22 teeth on the rear sprocket and 50 teeth on the front sprocket?
-
. quotient and remainder obtained when f(x) = x6 +3x5 + 4x2 -3x+2 is divided by g(x)= x2+2x-3 in Z7(x)
-
Project management involves the management of key resources which comprises of both human and other types of resources such as financial, infrastructure, equipment and other organisational assets. As...
-
1. Is it customary for a company like HCP to hold quarterly review meetings without having representation from its contractor, SoftSmart? 2. Can project status be determined without the use of...
-
write basic source code for a online bank balance checker using the folowing code? The Online Bank The Online Bank
-
You, CPA, work as the assistant controller for A-Plus Corp (A-Plus). You are currently working on a project with the mergers and acquisitions group, which is responsible for integrating the recent...
-
How can we mitigate the negative impacts of the grapevine in criminal justice organizations? Are there potential benefits to its presence in such organizations? Explain.
-
Dynamic Drilling provides the following information for 2021. Current Assets Total Assets Current Liabilities Total Liabilities Total Equity Net Cash from operating activities Sales Revenue Cost of...
-
You are maintaining a subsidiary ledger account for Police-Training Expenditures for 2013. The following columns are used: Inventory purchases are initially recorded as expenditures. Record the...
-
How much of each of the following prizes or awards is taxable? a. Cheline received a $50,000 gift bag at the Oscars in 2012. b. Jon received a gold watch worth $350 for 25 years of service to his...
-
Bill and Guilda each own 50 percent of the stock of Radiata Corporation, an S corporation. Guilda's basis in her stock is $25,000. On July 31, 2012, Bill sells his stock, with a basis of $40,000, to...
-
In 2012, Margaret and John Murphy are married taxpayers who file a joint tax return with AGl of $25,000. During the year they incurred the following expenses: Hospitalization insurance...
-
The following table presents the average price, in U.S. dollars per gallon, of unleaded regular gasoline in several countries in the years 2014 and 2016. a. Find the mean and median gas price for...
-
The following table presents the number of active breweries for samples of states located east and west of the Mississippi River. a. Find the mean number of breweries for states east of the...
-
The following table presents prices, in thousands of dollars, of single-family homes for some of the largest metropolitan areas in the United States for the third quarter of 2015 and the third...
Study smarter with the SolutionInn App