CS51 - Fall 2009 - Lecture 38
Main parts to algorithm analysis
- developing algorithms that work
- making them faster
- analyzing/understaning the efficiency/run-time
- Merge sort
- first, look at the merge method
- how can we use this method to sort numbers?
- how could we use this method to sort two numbers?
- can we repeat this idea?
- what is the runtime?
- look at layers
What if I wanted to sort something besides numbers, e.g. a particular object type?
- What information do all of these sorting method leverage?
- only leverage comparison between elements
- Comparable interface
- int compareTo(Object o)
- x.compareTo(y) returns something < 0 if x < y
- x.compareTo(y) returns 0 if x == y
- x.compareTo(y) returns something > 0 if x > y
- Some built in classes already implement Comparable
The easy way to sort in java:
- Array.sort(Comparable a) - Quicksort
- Collections.sort(List list) - Mergesort
- e.g. ArrayList
- an infinitely long strip of paper
- you have a reader that can read/write over one entry on the strip of paper at a time
- has a program that can only depend on that scanned symbol
- you have internal "states"
- turing machine functions by
- based on the current state and the symbol under the reader
- move to some different state
- possibly write something to the strip of paper
- transition to a state (possibly the same state)
- functionality: can do anything a computer can do