Lecture 35 (Tychonievich) - Search and Sort

Lecture Date: Friday, April 10

To design an algorithm, first make sure you can solve the problem yourself; then try to express your own actions in Java.

To search a list, we

  1. Look at each element in turn, stopping when/if we find it. In Java, 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. 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

From Lecture