======================================================= Introduction to Algorithm Lecture 1 ======================================================= Do not work alone, learn to collaborate. 25-30 min Do not waste your time, be productive. Do exercises to understand the materials. You should be able to explain your solution. ======================================================= Analysis of Algorithms The theoretical stusy of computer-program performance and resource usage What's more important than performance? maintainability, correctness, simplicity, stability, features, security, user friendlyness Why sduty algorithms and performance? Sometimes performance is related to user friendlyness. Real-time constraints feazible or not feazible Performance is like money, used to pay for other things like security and so on. Speed is fun. ========================================================= Problem sorting Input: a sequence(a1, a2, ..., an) of numbers Output: a permutation of the sequence (b1, b2, ..., bn) such that -> b1 <= b2 <= ... <= bn ======================================================== Insertion sort pseudocode indentation it's a bad idea to use indentation to represent the nesting of programs. ======================================================== Running Time => Depends on input (eg. already sorted; eg. reverse sorted) => Depends on input size (eg. 6 elem vs 6G) => upper bounds, a guarantee to users ======================================================== Kinds of analyses Worst-case: (usually) T(n) = maximum time of algorithm on any input of size n Average-case: (sometimes) T(n) = expected time of algorithm over all inputs of size n Need assumption of statistical distribution of inputs (probability of every input) Best-case: (bogus) Cheat with a slow algorithm that works fast on some input. ======================================================== What's ins sort w-c time? Depends on computer -ralative speed (on the same machine) -absolute speed (on diff machines) BIG IDEA Asymptotic analysis 1. Ignore machine dependent constraints 2. Look at growth Asymptotic notation balance mathematic understanding and engineering common sense ======================================================== Merge Sort Key Subroutine: merge establish a recurrence equality Recursion Tree In practice, merge sort beats insertion sort for n > 30 or so.