🔍

Lecture 3 - Algorithms

Introduction

input → □ → output

Running Time

Searching

String compare.

Data Structures

Sorting

We learnt about three different methods for sorting numbers

  1. Selection Sort
    1. Selection sort will begin by looking for the smallest number in the list and swap that number with our current position in the list.
    1. 5 2 7 4 1 6 3 0
      1. the algorithm will run through and identify the zero as the smallest, so it will get moved to the left
    1. 0 | 2 7 4 1 6 3 5
      1. because algo knows 0 is smallest, it will start from the next value and search for smallest value
      1. once it finds the one, it has to continue searching, because although its obvious this is the smallest value to a human, the machine has no way of knowing there is not another zero in array, so it has to methodically go through all values
    1. 0 1 | 7 4 2 6 3 5
      1. this same process will continue until all are sorted
  1. Bubble Sort
    1. Bubble sort is another sorting algorithm that works by repeatedly swapping elements to “bubble” larger elements to the end. In other words, it works through the list and sorts the two numbers next to each other to make sure they are in the correct order, breaking the list down into bubbles of two values.
    1. 5 2 7 4 1 6 3 0

      ^ ^

      if this list was to be sorted, first the five and two would be checked, and the five would get swapped to the right side

    1. 2 5 7 4 1 6 3 0

      ^ ^

      then the five and seven, which would need no changes

    1. 2 5 7 4 1 6 3 0

      ^ ^

      then the seven and four would be checked, and the seven would get moved to the right

    1. 2 5 4 7 1 6 3 0

      ^ ^

      and so on, with this first run through ending up with the 7 all the way to the right. In the second run-through, the algorithm would go all the way up to second last value, because it already knows the 7 is in correct position

Recursion

Merge Sort

  1. First, merge sort asks, “is this one number?” The answer is “no,” so the algorithm continues.

    7254

  1. Second, merge sort will now split the numbers down the middle (or as close as it can get)

    72|54

  1. Third, merge sort would look at these numbers on the left and ask, “is this one number?” Since the answer is no, it would then split the numbers on the left down the middle.

    7|2

  1. Fourth, merge sort will again ask , “is this one number?” The answer is yes this time! Therefore, it will quit this task and return to the last task it was running at this point.

    72|54

  1. Fifth, merge sort will sort the numbers on the left.

    27|54

  1. Now, the left side has been sorted a similar process will occur with the right-hand numbers. This will result in.

    27|45

  1. Both halves are now sorted. Finally, the algorithm will merge both sides. It will look at the first number on the left and the first number on the right. It will put the smaller number first, then the second smallest. The algorithm will repeat this for all numbers, resulting in:

    2457

Summary

Selection sort top, merge sort middle, bubble sort bottom