Unit 2

Runtime and Big-O Notation

[15.1] What is the Big-O notation of an algorithm whose runtime doubles with each additional element in the input set?

A. O(n)

B. O(n^2)

C. O(log n)

D. O(2^n)

[15.2] What is the best case for linear search? What is the worst case for linear search?

[15.3] An algorithm that checks each element of a list against every other element has a Big-O notation of _______.

[15.4] Explain why an algorithm with O(1) complexity is referred to as having constant time complexity.

[15.5] Consider the following code snippet. What is the time complexity?

i = 1
while i < n:
    i = i * 2

[15.6] What is the Big-O notation for an algorithm that makes two separate passes through a list of n elements?

[15.7] Given the following Python function, what is its Big-O complexity?

def find_pairs(numbers, target_sum):
    count = 0
    for i in range(len(numbers)):
        for j in range(5, len(numbers)):
            if numbers[i] + numbers[j] == target_sum:
                count += 1
    return count

[15.8] Why is the worst-case time complexity often more significant than the best-case complexity when analyzing algorithms?

[15.9] Given the following Python function, what is its time complexity?

def process_data(data):
    total = 0
    for i in range(0, len(data), 2):  # increment by 2
        total += data[i]
    return total

[15.10] The Big-O notation O(n^2 + n) simplifies to O(n^2)? True or False?

[15.11] Is the Big-O notation used to express the best-case performance of an algorithm?

[15.12] Given the following Python function, what is its time complexity?

def varied_loops(n):
    count = 0
    for i in range(n):
        for j in range(1, 100):
            count += 1
    return count

[15.13] Given the following Python function, what is its time complexity?

def mixed_operations(n):
    sum = 0
    for i in range(n):
        sum += i
        k = n
        while k > 0:
            k //= 2
    return sum

[15.14] Given the following recursive Python function, what is its time complexity?

def recursive_function(n):
    if n == 1:
        return n
    else:
        return recursive_function(n - 1)

[15.15] Given the following recursive Python function, what is its time complexity?

def recursive_function(n):
    if n <= 1:
        return n
    else:
        return recursive_function(n - 1) + recursive_function(n - 2)

[15.16] Given the following recursive Python function, what is its time complexity?

def complex_loops(n):
    total = 0
    for i in range(n):
        for j in range(n):
            for k in range(n):
                total += i * j * k
    return total

[15.17] Given the following Python function, what is its time complexity?


1. def sumEvens(lst): # n = len(lst)
2.    result = 0
3.    for i in range(len(lst)):
4.        if lst[i] % 2 == 0:
5.          result = result + lst[i]
6.    return result

[15.18] Given the following Python function, what is its time complexity?


1: def example(lst): # n is len(lst)
2:      result = []
3:      for i in range(0, len(lst), 2):
4:          if lst[i] != lst[i+1]:
5:              average = (lst[i] + lst[i+1]) / 2
6:              if average in lst:
7:                  result.append(average)
8:      return count

[15.19] Given the following recursive Python function, what is its time complexity?


def countdown(n): # n = n
 if n <= 0:
    print("Finished!")
 else:
    print(n)
    countdown(n - 5)

[15.20] What is the runtime of the following code block?


def swap(lst, i, j): # n = len(lst)
 tmp = lst[i]
 lst[i] = lst[j]
 lst[j] = tmp

[15.21] What is the runtime of the following code block?


def countDigits(n): # n = n
 count = 0
 while n > 0:
    n = n // 15
    count = count + 1
 return count

[15.22] What is the runtime of the following code block?


def count(n): # n = n
 for i in range(n, 0, -10):
    print(i)

[15.23] What is the runtime of the following code block?


def example(x): # n = x
 x = x + 5
 y = x + 2
 print(x, y)

[15.24] What is the runtime of the following code block?


def printContains(lst, x): # n = len(lst)
 result = linearSearch(lst, x)
 print(x, "in lst?", result)

[15.25] What is the runtime of the following code block?


def printContains(lst, x): # n = len(lst)
 result = binarySearch(lst, x)
 print(x, "in lst?", result)

[15.26] What is the runtime of the following code block?


def countAll(lst): # n = len(lst)
 for i in range(len(lst)): 
    count = lst.count(i) 
    print(i, "occurs", count, "times")