Search and Sorting

https://slides.com/georgelee/ics141-algorithms-2/live

Recap

Searching

Linear Search

We go through each item in the array. If we find it, we return the index. Else, we return -1.

def search(value, numbers):
    for index, num in enumerate(numbers):
        if num == value:
            return index
        
    return -1

Binary Search

Assume the array is sorted. We look at the middle value of the array.

 

If it matches the value we're looking for, yay!

If the value is larger than the one we're searching for, we look in the first half.

Else, we look in the other half of the array.

Array [1 3 6 7 9], Find 9

 

Iteration 1:

Start: 0, End: 4, Middle: 2 -> [1 3 6 7 9] -> [1 3 6 7 9]

 

Iteration 2:

Start: 3, End: 4, Middle: 3 -> [1 3 6 7 9] -> [1 3 6 7 9]

 

Iteration 3:

Start 4, End: 4, Middle: 4 -> [1 3 6 7 9]

 

Result = 4

 

Array [1 3 6 7 9], Find 2

 

Iteration 1:

Start: 0, End: 4, Middle: 2 -> [1 3 6 7 9] -> [1 3 6 7 9]

 

Iteration 2:

Start: 0, End: 1, Middle: 0 -> [1 3 6 7 9] -> [1 3 6 7 9]

 

Iteration 3:

Start 1, End: 1 -> [1 3 6 7 9]

 

​Start 1, End: 0 ->  -1

 

Sorting

Sorting a list of numbers

  • A rite of passage for CS students
  • Given a list of numbers, sort them.
  • Non-recursive solutions

Bubble Sort

Given an array, we iterate from left to right. We compare numbers adjacent to each other. If the one on the left is bigger than the one on the right, we swap them.

 

The largest number "bubbles" to the end of the array. We iterate again to find the next biggest number.

 

Keep going until the array is sorted.

Sort the array [1, 4, 7, 2, 6]

Iteration 1:

[1 4 7 2 6] -> [1 4 7 2 6] -> [1 4 2 7 6] -> [1 4 2 6 7]


Iteration 2:

[1 4 2 6 7] -> [1 2 4 6 7] -> [1 2 4 6 7]


Iteration 3:

[1 4 2 6 7] -> [1 2 4 6 7]


Iteration 4:

[1 2 4 6 7]
def bubble_sort(array):
    for i in range(0, len(array) - 1):
        for j in range(0, len(array) - i - 1):
            if array[j] > array[j + 1]:
                array[j], array[j + 1] = array[j + 1], array[j]
                
    return array

Bubble Sort

Not even once

Insertion Sort

Split the array into a sorted and unsorted. Take the first unsorted value and "insert" it into the sorted array. Keep going until the entire array is sorted. Think about being dealt cards.

 

When inserting, the book goes from left to right.

 

My implementation will go from right to left.

 

Sort the array [3 1 7 4 2]

Iteration 1:

[3 1 7 4 2] -> [1 3 7 4 2] -> [1 3 7 4 2]

 

Iteration 2:

[1 3 7 4 2] -> [1 3 7 4 2]

 

Iteration 3:

[1 3 7 4 2] -> [1 3 4 7 2] -> [1 3 4 7 2]

 

Iteration 4:

[1 3 4 7 2] -> [1 3 4 2 7] -> [1 3 2 4 7] -> [1 2 3 4 7] -> [1 2 3 4 7]
def insertion_sort(array):
    # Assume first element is "sorted"
    for i in range(1, len(array)):
        value = array[i]
        position = i
        for j in range(i, -1, -1):
            if array[j - 1] <= value:
                break
                
            array[j] = array[j - 1]
            
        array[j] = value
                
    return array

Videos on Sorting

Greedy Algorithms

Greedy Algorithm

The idea is that whenever we are faced with a choice, we take the best one. We typically use these algorithms to solve optimization problems.

 

* Making Change

* The "Knapsack" problem

Made with Slides.com