Question
For each of the following functions code, calculate T(n), the running time it takes to solve a problem of size n, and find the corresponding
- For each of the following functions code, calculate T(n), the running time it takes to solve a problem of size n, and find the corresponding Big-O notation:
- Sequential search
def sequentialSearch(alist, item): | |
pos = 0 | |
found = False | |
while pos < len(alist) and not found: | |
if alist[pos] == item: | |
found = True | |
else: | |
pos = pos+1 | |
return found |
- Bubble sort
def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
- Binary search
def binarySearch (alist, item):
first=0
last=len(alist)-1
found=False
While first<=last and not found
midpoint=(first+last)/2
If alist[midpoint]==item:
found=True
else:
if item last=midpoint-1 else: first=midpoint+1 return found
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