Answered step by step
Verified Expert Solution
Question
1 Approved Answer
# function to read value from a file into an array def read _ file _ to _ array ( fileName ) : with open
# function to read value from a file into an array
def readfiletoarrayfileName:
with openfileNamer as file: # opens the file in read mode and automatically closes
# reads each line of file
array linestrip for line in file
return array
# function to implent a binary search algorithm
def binarysearcharray target:
counter
left
right lenarray
# loop to binary search
while left right:
counter # counts how many iterations it took to get target value
mid left right # middle element
current intarraymid
# checks if current value equals to the targeted value
if current target:
return mid, counter
elif current target: # checks if the value is too high than target
right mid
else: # checks if value is too low
left mid
# returns if the target is not found, as well as the # of iterations
return counter
target
fileName 'numbers.txt
# reads the values from the file into an array
array readfiletoarrayfileName
# performs binary search
result, counter binarysearcharray target
# outputs the results and # of iterations
if result :
printfTarget target found at index result with counter iterations."
else:
printfTarget target not found after counter iterations."
What is the worst case time complexity? Evaluate line by line, create a time complexity function and then define its Big O value.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started