1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
yKpoI14uk [10]
3 years ago
13

Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorit

hm to locate one of the values. The module should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another module that uses the binary search algorithm to locate the same value. It should also keep a count of the number of comparisons it makes. Display these values on the screen.
Computers and Technology
1 answer:
PIT_PIT [208]3 years ago
4 0

Answer:

  1. def sequenceSearch(myList, target):
  2.    count = 0
  3.    for x in myList:
  4.        count += 1
  5.        if(x == target):
  6.            return count  
  7.    
  8.    return -1
  9. def binarySearch(myList, target):
  10.    minIndex = 0
  11.    maxIndex = len(myList) - 1
  12.    count = 0
  13.    while(maxIndex >= minIndex):
  14.        middleIndex = minIndex + (maxIndex - minIndex)//2
  15.        count += 1
  16.        if(target < myList[middleIndex]):
  17.            maxIndex = middleIndex - 1
  18.        elif(target > myList[middleIndex]):
  19.            minIndex = middleIndex + 1
  20.        else:
  21.            return count  
  22.    return -1
  23. myList = [4, 7, 9, 12, 15, 19, 21, 40, 57, 80, 91, 111, 123, 127, 145, 167, 178, 180, 210, 222]
  24. print(sequenceSearch(myList, 210))
  25. print(binarySearch(myList, 210))

Explanation:

Firstly, create a sequential search function that will take two parameter, list and target number (Line 1). In the function, create a counter variable, count and initialize it with zero (Line 2). Next create a for loop to start the sequential search from the first element of list and keep tracking of count (Line 4 -7). If target is found in the list, return the count (Line 7) otherwise return -1.

Next, create another function for binary search that will take the same input parameters as sequential search function (Line 11). In the binarySearch function, use the similar counter variable, count to track the number of searching (Line 14).  Next, use a while loop to implement the binary search algorithm that will keep comparing target against the list element in middle index (Line 20 - 25). If target is smaller than middle value adjust the maxIndex by having middleIndex -1. If target bigger than the middle value, set the minIndex to middleIndex - 1. Otherwise it will return the count. If the target is not found, return -1 (Line 27).

In the main program, create a sample list with 20 elements and then test the sequenceSearch and binarySearch function ny using the sample list and target = 210 as arguments. We shall get the output: 19 4

You might be interested in
Do you want my hero academia?<br><br><br><br><br><br> if so, who's your favorite character :&gt;
VMariaS [17]

Shoto Todoroki and Katsuki Bakugo

4 0
3 years ago
Read 2 more answers
In a relational database, the three basic operations used to develop useful sets of data are:_________.
olga_2 [115]

In a relational database, the three basic operations used to develop useful sets of data are:

\sf\purple{a.\: Select, \:project,\: and\: join. }

\large\mathfrak{{\pmb{\underline{\orange{Mystique35 }}{\orange{❦}}}}}

6 0
3 years ago
Which slideshows design can not be use?
Readme [11.4K]

Answer:

Is there suppose to be a pic ?

Explanation:

7 0
3 years ago
Bugs bunny personality traits
Dafna1 [17]

Answer:

As a character, Bugs Bunny is king, and he's as close to an animated culture hero as we're going to get. Think about it. He's the person you want to be — the smartest one in the room who's still effortlessly cool. He's quick-witted, funny, and even a little cruel, but only to his tormenters.

Explanation:I hope this helps!!!Plz leave a heart and a rating!!

3 0
4 years ago
Which item is used in Excel to identify the row of a particular cell? letter number type sheet
Alik [6]

It can be any letter or number as long as its validated.

7 0
4 years ago
Read 2 more answers
Other questions:
  • If you see ##### in a cell, you should
    10·1 answer
  • What computing platform was designed to help a beginner create interactive objects that receive input from sensors and use that
    11·1 answer
  • Media applications that play audio or video files are part of a class of workkloads called "streaming" workloads (i.e., they bri
    9·1 answer
  • Asymmetric key encryption combined with the information provided by a. certificate authority allows unique identification of the
    8·1 answer
  • Which statement best describes desktop publishing?
    10·1 answer
  • Clocks (also called timers) are essential to the operation of any multiprogrammed system. They maintain the time of day, and pre
    9·1 answer
  • Consider the conditions and intentions behind the creation of the internet—that it was initially created as a tool for academics
    12·1 answer
  • What structure is a candle​
    13·1 answer
  • Software created according to user choice​ true or false
    5·1 answer
  • _____________ do not contribute to effective group work.
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!