Ask Question
15 July, 19:18

Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm 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.

+2
Answers (1)
  1. 15 July, 22:08
    0
    def sequenceSearch (myList, target) : count = 0 for x in myList: count + = 1 if (x = = target) : return count return - 1 def binarySearch (myList, target) : minIndex = 0 maxIndex = len (myList) - 1 count = 0 while (maxIndex > = minIndex) : middleIndex = minIndex + (maxIndex - minIndex) / /2 count + = 1 if (target myList[middleIndex]) : minIndex = middleIndex + 1 else: return count return - 1 myList = [4, 7, 9, 12, 15, 19, 21, 40, 57, 80, 91, 111, 123, 127, 145, 167, 178, 180, 210, 222] print (sequenceSearch (myList, 210)) 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
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm to ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers