CS 228 meeting -*- Outline -*- * brief overview of sorting (HR 12.7-8) The sorting algorithms in this part of the book rely more on algorithms, as opposed to a data structures, so we'll just look at it quickly ** motivating question ------------------------------------------ SORTING (HR 12.7-8) MOTIVATING PROBLEM Given a random access collection, arrange them in nondecreasing order. extern void Sort( int vec[], int size ); // PRE: size >= 0 // && 0..size-1 are legal indexes of vec // MODIFIES: vec[0..size-1] // POST: vec[0..size-1] contains the // same values as vec[0..size-1] // but sorted into nondecreasing order ------------------------------------------ ** overview ------------------------------------------ SORTING OVERVIEW Average Worst case Techniques Time cost time cost A. bubble O(N^2) O(N^2) B. selection O(N^2) O(N^2) C. quicksort O(N log N) O(N^2) Benchmark Bubble Selection Quicksort 1000 random 127.4 80.86 2.52 1000 sorted 0.16 85.2 1.43 Key insight: Divide and conquer ------------------------------------------ the insight has very wide application! Caveat: bubble sort is actually ok if have a small number of data or if data are already sorted.