Unit 2

Recursion II and Search Algorithms

[13.1] For each of the following lists, would binary or linear search run faster to find 110?

a) [110, 104, 83, 249, 50]


b) [15, 11, 49, 400, 110, 90, 64]


c) [1, 4, 6, 35, 110, 120, 400, 500]


d) [15, 11, 49, 400, 90, 64]


e) [30, 42, 56, 79, 130, 200]


[13.2] Write, in order, the elements checked to find 6 in the following list for both linear and binary search: L = [1,5,6,10,11,12].

[13.3] Answer the following questions based on the recursive binary search code you have seen in lecture. Write down the original function call and every subsequent recursive call at each step when doing binary search for different targets on the following list: [0, 3, 5, 8, 9, 10 11, 14, 17]

  1. Target: 11
  2. Target: 7

[13.4] What is the Big-O time complexity for binary search? What about linear search?

[13.5] What is the worst case for linear search? What about binary search?

[13.6] What is the best case for linear search? What about for binary search?

[13.7] What is the difference between check-any and check-all patterns as shown below?

 
def checkAny(lst, target):
    for i in range(len(lst)):
        if lst[i] == target:
            return True
    return False
        
def checkAll(lst, target):
    for i in range(len(lst)):
        if lst[i] != target:
            return False
    return True
    

[13.8] Recall the towers of hanoi problem and code from lecture. What value will the result have?


# Prints instructions to solve Towers of Hanoi and
# returns the number of moves needed to do so.
def moveDiscs(start, tmp, end, discs):
 if discs == 1: # 1 disc - move it directly
    print("Move one disc from", start, "to", end)
    return 1
 else: # 2+ discs - move N-1 discs, then 1, then N-1
    moves = 0
    moves = moves + moveDiscs(start, end, tmp, discs - 1)
    moves = moves + moveDiscs(start, tmp, end, 1)
    moves = moves + moveDiscs(tmp, start, end, discs - 1)
    return moves

result = moveDiscs("left", "middle", "right", 3)

[13.9] Implement linear search with recursion. Return the target if it is found, otherwise return None.

[13.10] Implement binary search with recursion. Return True if target is found otherwise False

[13.11] Write a function, fibonacci that generates the nth Fibonacci number using recursion, where n is a parameter that the function takes in.

The fibonacci sequence is defined recursively below:

fib(0) = 0
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2) 

Examples

fibonacci(0) = 0

fibonacci(1) = 1

fibonacci(2) = 1

fibonacci(3) = 2

fibonacci(6) = 8

Hint: What are the base cases for the Fibonacci sequence (there are multiple!) Look at the formula to get an idea about the recursive case

[13.12] Given the function below, trace through the code to find the output for the following function call: mystery_function(5)


def mystery_function(n):
    if n == 0:
        return 0
    else:
        if n % 2 == 0:
            return (n * n) + mystery_function(n - 1)
        else:
            return mystery_function(n - 1)