recitation08

# recitation08 - MIT OpenCourseWare http/ocw.mit.edu 6.006...

This preview shows pages 1–13. Sign up to view the full content.

MIT OpenCourseWare http://ocw.mit.edu 6.006 Introduction to Algorithms Spring 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6.006 Recitation Build 2008.16
Coming up next. .. Sorting Scenic Tour: Insertion Sort, Selection Sort, Merge Sort New Kid on the Block: Merge Sort Priority Queues Heap-Based Implementation

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sorting Input: array a of N keys Output: a permutation a s of a such that a s [k] < a s [k+1] Stable sorting:
Sorting Maybe the oldest problem in CS Reﬂects our growing understanding of algorithm and data structures Who gives a damn? All those database tools out there

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sorting Algorithms: Criteria What Why Speed That’s what 6.006 is about Auxiliary Memory External sorting, memory isn’t that cheap Simple Method You’re learning / coding / debugging / analyzing it # comparisons, data moving Keys may be large (strings) or slow to move (ﬂash memory)
Insertion Sort Base: a[0:1] has 1 element is sorted Induction: a[0:k] is sorted, want to grow to a[0:k+1] sorted ﬁnd position of a[k+1] in a[0:k] insert a[k+1] in a[0:k] 5 8 2 7 1 4 3 6 5 8 2 7 1 4 3 6 2 5 8 7 1 4 3 6 2 5 7 8 1 4 3 6 1 2 5 7 8 4 3 6 1 2 4 5 7 8 3 6 1 2 4 5 7 8 3 6 1 2 3 4 5 6 7 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Insertion Sort: Costs Find position for a[k+1] in a[0:k] - O(log(k)) use binary search Insert a[k+1] in a[0:k]: O(k) shift elements Total cost: O(N log(N)) + O(N 2 ) = O(N 2 ) Pros: Optimal number of comparisons O(1) extra memory (no auxiliary arrays) Cons: Moves elements around a lot
Selection Sort Base case: a[0:0] has the smallest 0 elements in a Induction: a[0:k] has the smallest k elements in a, sorted; want to expand to a[k+1] ﬁnd min(a[k+1:N]) swap it with a[k+1] 5 8 2 7 1 4 3 6 1 8 2 7 5 4 3 6 1 2 8 7 5 4 3 6 1 2 3 7 5 4 8 6 1 2 3 4 5 7 8 6 1 2 3 4 5 7 8 6 1 2 3 4 5 6 8 7 1 2 3 4 5 6 7 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Selection Sort: Costs ﬁnd minimum in a[k+1:N]) - O(N-k) scan every element swap with a[k] - O(1) need help for this? Total cost: O(N 2 ) + O(N) = O(N 2 ) Pros: Optimal in terms of moving data around O(1) extra memory (no auxiliary arrays) Cons: Compares a lot
Merge-Sort 1. Divide Break into 2 sublists 2. Conquer 1-elements lists are sorted 3. Proﬁt Merge sorted sublists 5 8 2 7 1 4 3 6 5 8 2 7 1 4 3 6 5 8 2 7 1 4 3 6 2 5 7 8 1 3 4 6 1 2 3 4 5 6 7 8 There is no step 6 There is no step 7 There is no step 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Merge-Sort: Cost You should be ashamed of if you don’t know!
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 50

recitation08 - MIT OpenCourseWare http/ocw.mit.edu 6.006...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online