Question: Test the program for array sizes N = 16, 32, 64, 128, 256, 512, 1024, 2048, , 225. Initialize the array with random numbers between

Test the program for array sizes N = 16, 32, 64, 128, 256, 512, 1024, 2048, ……, 225. Initialize the array with random numbers between the ranges 1 through N and use the same array for testing linear search and binary search. Remember to sort the array before using binary search. Use a text file with 1,000 random numbers in the range 1 through 225 as the search keys.

Compare the execution time for linear search and binary search. Include the time taken for sorting with the binary search time (you have to sort only once for each array size). Use a table or plot to summarize the results and document your observations and explanations in the report.

Linear Serch (Python)

def linSearch(array, number):
    for x in range(0, len(array)):
        if number==array[x]:
            print(str(number) + " was found here at " + str(x))
           
sample_array = [6,9,8,10,7]
test = linSearch(sample_array, 7)
print(test)

 

Binary Search (Python)

def binSearch(sorted_array, number, min=0, max=None):
    max = max or len(sorted_array)-1
    if max < min:
        return False
    else:
        mid = min+ (max-min)/2
        mid1 = int(mid)
        if mid>number:
            return binSearch(sorted_array, number, min,
mid1-1)
        elif mid            return binSearch(sorted_array, number,
mid1+1, max)
        else:
            return mid1
           
           
           
sample_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print((binSearch(sample_array, 9)) -1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Code in python import random import time def linearsearcharrk for i in rangelenarr if arrik return ... 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

Document Format (2 attachments)

PDF file Icon

635e069e221ca_180832.pdf

180 KBs PDF File

Word file Icon

635e069e221ca_180832.docx

120 KBs Word File

Students Have Also Explored These Related Algorithms Questions!