06-ch6-sorting.ppt

06-ch6-sorting.ppt - CS 4102, Algorithms: More Sorting Lets...

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

View Full Document Right Arrow Icon
CS 4102, Algorithms: More Sorting • Let’s finish some sorting problems • Chapter 6 in the textbook • Insertion Sort, Quicksort • Lower bound for sorting using key comparisons
Background image of page 1

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

View Full DocumentRight Arrow Icon
Reminder: Common Forms of Recurrence Equations • Remember these? • Divide and conquer: Like Mergesort T(n) = bT(n/c) + f(n) • Solve directly or apply master theorem • Chip and conquer: T(n) = T(n-c) + f(n) • Note: One subproblem of lesser cost! Insertion sort will be like this. • Chip and Be Conquered: T(n) = b T(n-c) + f(n) where b > 1 • Like Towers of Hanoi • Exponential! See recursion tree argument on p. 140
Background image of page 2
Insertion Sort • The strategy: 1.First section of list is sorted (say i-1 items) 2.Increase this partial solution by… 3.Shifting down next item beyond sorted section (i.e. the ith item) down to its proper place in sorted section. (Must shift items up to make room.) 4.Since one item alone is already sorted, we can put steps 1-3 in a loop going from the 2nd to the last item. • Note: Example of general strategy: Extend a partial solution by increasing its size by one. (Possible name: decrease and conquer )
Background image of page 3

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

View Full DocumentRight Arrow Icon
Insertion Sort: Pseudocode from text insertion_sort(a) { n = a.last for i = 2 to n { val = a[i] // save a[i] so it can be inserted j = i – 1 // into the correct place // if val < a[j],move a[j] right to make room for a[i] while (j 1 && val < a[j]) { a[j + 1] = a[j] j = j - 1 } a[j + 1] = val // insert val } }
Background image of page 4
Insertion sort in Python def insertion_sort(list): n = len(list) for i in range(1,n): val = list[i] j = i-1 while j >= 0 and val < list[j]: list[j+1] = list[j] j = j-1 list[j+1] = val return
Background image of page 5

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

View Full DocumentRight Arrow Icon
Properties of Insertion Sort • Easy to code • In-place • What’s it like if the list is sorted? • Or almost sorted? • Fine for small inputs • Why?
Background image of page 6
Insertion Sort: Analysis • Worst-Case: • Average Behavior • Average number of comparisons in inner-loop? • So for the i th element, we do roughly i/2 comparisons • To calculate A(n), we note i goes from 2 to n-1 • Best-case behavior? One comparison each time W ( n ) = i = n ( n 1) /2 = Θ ( n 2 i = 1 n 1 ) i ( i + 1) j + i ( i + 1) = j = 1 i i 2 + 1 1 ( i + 1) B ( n ) = 1 = n 1 i = 1 n 1 A ( n ) = i 2 + 1 1 ( i + 1) Λ Ν Μ Ξ Π Ο n 2 4 i = 2 n 1
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: Best of a breed? • We know that other I.S. is one of many quadratic sort algorithms, and that log-linear sorts (i.e. Θ (n lg n ) ) do exist • But, can we learn something about I.S. that tells us what it is about I.S. that “keeps it” in the slower class? • Yes, by a lower-bounds argument on a restricted set of sort algorithms • BTW, this is another example to show you how to make arguments about lower-bounds
Background image of page 8
Removing Inversions • Define an inversion in a sequence: A pair of elements that are out of order • E.g. { 2, 4, 1, 5, 3 } not sorted and has 4 inversions: pairs (2,1) (4,1) (4,3) (5,3) • To sort, we must fix each of these • What’s the maximum possible number of inversions? n(n-1)/2
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/21/2010 for the course CS 445 taught by Professor Bloomfield,a during the Spring '08 term at UVA.

Page1 / 37

06-ch6-sorting.ppt - CS 4102, Algorithms: More Sorting Lets...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online