CSC 203 Final: Recursion, Searching, Sorting

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/21

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

22 Terms

1
New cards

Recursion

a function that calls itself from within. It helps to visualize a complex problem into repeatable basic steps.

2
New cards

Base condition (base case)

the condition that tells a recursive function when to stop executing and return a value.

3
New cards

Divide and conquer

breaking a large problem into smaller subproblems that are easier to solve one at a time.

4
New cards

Factorial

the sum of multiplying integers from n down to 1. n is represented as n!

6 would be 6 x 5 x 4 x 3 x 2 x 1 = 720

5
New cards

What happens when you recursively find a factorial?

Create a function called factorial that has an input of n. We want to find the factorial of n.

  • First, create a base case for this function. We want to multiply n by every number from n-1 all the way down to 1. So our base case is if n <= 1: return 1

  • For the most part, this line (base case) is going to be skipped until we get to 1. But until we get to 1, we need to multiply every number on our way down to 1. 

  • We will need to return our current number else: n * factorial(n-1).

  • The function keeps calling itself and reducing n by 1 since factorial(n-1) would be unknown initially.

  • Nothing is calculated until the base case is reached.

  • Once n == 1, the function starts returning values back up the call stack.

  • Each return multiplies the current number by the result from the previous call until n! is reached.

6
New cards

Recursion: factorial function

def f(n):

    if n <= 1:

        return 1

    else:

        return n * f(n - 1)

7
New cards

Fibonacci sequence

Begins with 0 and 1. Thereafter, each term is the sum of the previous two terms.

  • 0, 1, 1, 2, 3, 5, 8, 13, 21

0 + 1 = 1

0, 1 + 1 = 2

0, 1, 1 + 2 = 3

0, 1, 1, 2 + 3 = 5

0, 1, 1, 2, 3 + 5 = 8

0, 1, 1, 2, 3, 5 + 8 = 13

0, 1, 1, 2, 3, 5, 8 + 13 = 21

8
New cards

The Fibonacci sequence may be defined recursively as follows:

  • fibonacci(0) is defined to be 0

  • fibonacci(1) is defined to be 1

  • fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)

9
New cards

There are two base cases for the fibonacci calculation:

  • fibonacci(0) is 0, and

  • fibonacci(1) is 1

10
New cards

What happens when you recursively find a fibonacci sequence?

Tests base cases: If n == 0 or n == 1: return n because fibonacci(0) is 0 and fibonacci(1) is 1

else:

  • Fibonacci is defined as current number = previous number + the one before that. If you want the nth Fibonacci number: n - 1 gives you the previous Fibonacci number and n - 2 gives you the one before the previous. Add them up

11
New cards

Recursion: fibonacci function

def f(n):

    if n==0 or n==1: #or if n in (0,1)

        return n

    else:

        return f(n-1) + f(n-2)

12
New cards

Linear search

Searches each element in an array sequentially.

Start from the leftmost element and one by one compares the search key with each element of the array.

  • If the search key matches with an element, return its index.

  • If there are duplicate values in the array, the linear search returns the index of the first element that matches the search key.

  • If the search key doesn't match with any of the elements, return -1.

13
New cards

enumerate()

adds a counter to each item in a list or any other iterable, and returns a list of tuples containing the index position and the element for each element of the iterable. 

  • It turns the iterable into something we can loop through using indexes, where each item comes with its number (starting from 0 by default). 

x = ('apple', 'banana', 'cherry')

y = enumerate(x)

print(list(y))

[(0, 'apple'), (1, 'banana'), (2, 'cherry')]

14
New cards

Linear search function

def f(data, search_key):

    for index, value in enumerate(data):

        if value == search_key:

            return index

    return -1

15
New cards

Search key

the value being searched for in a list or array.

16
New cards

Sentinel value

a special value (such as -1) returned when a search key is not found.

17
New cards

Selection sort

In ascending order: its first iteration selects the lowest value in the array and swaps it with the first element. The second iteration selects the second-lowest value (which is in the unsorted portion to the right of the sorted portion) and swaps it with the second element. The algorithm continues until the numbers are in increasing order

<p>In ascending order: <span style="background-color: transparent; font-family: &quot;Josefin Sans&quot;, sans-serif;"><span>its first iteration selects the </span></span><span style="background-color: transparent;"><span>lowest value </span></span><span style="background-color: transparent; font-family: &quot;Josefin Sans&quot;, sans-serif;"><span>in the array and swaps it with the first element. The second iteration selects the second-lowest value (which is in the unsorted portion to the right of the sorted portion) and swaps it with the second element. The algorithm continues until the numbers are in increasing order</span></span></p>
18
New cards

Selection sort function

def selection_sort(data) :

    for index1 in range(len(data) - 1):

smallest = index1 # first index of remaining array

for index2 in range(index1 + 1, len(data)) :

    if data[index2] < data[smallest]:

        smallest = index2

data[smallest], data[index1] = data[index1], data[smallest]

print_ pass(data, index1 + 1, smallest)

19
New cards

Insertion sort

In ascending order: if you’re given an array

  1. Work left to right

  2. Examine each item and compare it to items on its left

  3. Insert the item in the correct position in the array

The array will form sorted and unsorted portions.

<p>In ascending order: <span style="background-color: transparent; font-family: &quot;Josefin Sans&quot;, sans-serif;"><span>if you’re given an array</span></span></p><ol><li><p><span style="background-color: transparent; font-family: &quot;Josefin Sans&quot;, sans-serif;"><span>Work left to right</span></span></p></li><li><p><span style="background-color: transparent; font-family: &quot;Josefin Sans&quot;, sans-serif;"><span>Examine each item and compare it to items on its left</span></span></p></li><li><p><span style="background-color: transparent; font-family: &quot;Josefin Sans&quot;, sans-serif;"><span>Insert the item in the correct position in the array</span></span></p></li></ol><p><span style="background-color: transparent; font-family: &quot;Josefin Sans&quot;, sans-serif;"><span>The array will form sorted and unsorted portions.</span></span></p>
20
New cards

Insertion sort function

def insertion_sort(data):

    #loop over len(data) - 1 elements

    for next in range(1, len(data)):

insert = data[next] # value to insert

move_item = next # location to place element

    #search for place to put current element

    while move_item › 0 and data [move_item - 1] > insert:

        #shift element right one slot

        data [move_item] = data [move_item - 1]

         move_item -= 1

    data[move_item] = insert # place inserted element

    print_pass(data, next, move_item) #output pass of algorithm

21
New cards

Sorted portion

the part of the array that is already sorted.

22
New cards

Unsorted portion

the part of the array that has not yet been sorted.