Date

Lecture Date: Wednesday, November 16

Today we'll look at four particular algorithms that have been important in computing (well... at least in CS1) for a very long time.

Specifically:

We'll do some in-class activities to show exactly how these algorithms work. We won't really look at code yet - the purpose is to figure out just how "good" each of these algorithms is in various circumstances.

To search a list, we could:

  1. Look at each element in turn, stopping when/if we find it. In python, this process becomes a simple for loop and is called Linear Search. If the list has n elements, the loop might repeat as many as n times.

  2. If we know the list is in some kind of order, look at the middle element and then consider only half the list, repeating until we find the element we want. This is called Binary Search If the list has n elements, we can cut it in half at most log2(n) times.

To sort a list, we could:

  1. Compare neighboring elements, swapping them if they are out of order. This is called Bubble Sort. In general, this requires us to look across the list repeatedly; roughly speaking, we have to walk the entire list (n elements) n times, so the program might take as much as n2 times.

  2. Find the smallest value and move it to a new list, the repeat until we can moved them all. This is called Selection Sort. Finding an element (linear search) takes n time, and we have n of them to find, so we are still in n2 time overall, like bubble sort.

  3. Take the next item in the list and then move it to the correct place by shifting the previous items. This is called Insertion Sort. Moving down the list (linear search) takes n time, and we may have to move n of them to find the right spot, so we are still in n2 time overall, like bubble sort.

  4. Split the list in half, sort each half, and then merge the two halves in order. This is called Merge Sort. Merging in takes time proportional to n, and we can split at most log2(n) times, for a total of n log2(n) time.

As a point of context,

  • There are around 8,000,000,000 people on Earth
  • Suppose we can handle a million people per second
  • Then we expect

    • linear search to take 8,000 seconds or about 2 hours
    • binary search to take 0.000033 seconds
    • bubble or selection sort to take over a trillion years, more than 70 times larger than the current estimated age of the universe
    • merge sort to take 264,000 seconds or about 3 days

    Note that all of these are approximate, but they within an order of magnitude of the right answers