anguage is c++.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Language is c++.

 

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!
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!
Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY