recitation08

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

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

View Full Document Right Arrow Icon
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 .
Background image of page 1

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

View Full DocumentRight Arrow Icon
6.006 Recitation Build 2008.16
Background image of page 2
Coming up next. .. Sorting Scenic Tour: Insertion Sort, Selection Sort, Merge Sort New Kid on the Block: Merge Sort Priority Queues Heap-Based Implementation
Background image of page 3

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

View Full DocumentRight Arrow Icon
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:
Background image of page 4
Sorting Maybe the oldest problem in CS Reflects our growing understanding of algorithm and data structures Who gives a damn? All those database tools out there
Background image of page 5

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

View Full DocumentRight Arrow Icon
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 (flash memory)
Background image of page 6
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 find 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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] find 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
Selection Sort: Costs find 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
Background image of page 10
Merge-Sort 1. Divide Break into 2 sublists 2. Conquer 1-elements lists are sorted 3. Profit 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
Background image of page 11

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

View Full DocumentRight Arrow Icon
Merge-Sort: Cost You should be ashamed of if you don’t know!
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online