This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 4102, Algorithms: More Divide and Conquer Read: Algorithms text, Chapter 5 Examples: Mergesort Trominos Closest Pair of Points Strassens Matrix Multiplication Algorithm New Problem: Sorting a Sequence The problem: Given a sequence a a n reorder them into a permutation a a n such that a i <= a i+1 for all pairs Specifically, this is sorting in nondescending order Basic operation Comparison of keys. Why? Controls execution, so total operations often proportional Important for definition of a solution Often an expensive operation (say, large strings are keys) However, swapping items is often expensive We can apply same techniques to count swapping in a separate analysis Why Do We Study Sorting? An important problem, often needed Often users want items in some order Required to make many other algorithms work well. Example: For searching on sorted data by comparing keys, optimal solutions require (log n) comparisons using binary search And, for the study of algorithms A history of solutions Illustrates various design strategies and data structures Illustrates analysis methods Can prove something about optimality Mergesort is Classic Divide & Conquer Mergesort Strategy Algorithm: Mergesort Specification: Input: Array list and indexes first, and Last, such that the elements list[i] are defined for first <= i <= last. Output: list[first], , list[last] is sorted rearrangement of the same elements Algorithm: def mergesort(list, first, last): if first < last: mid = (first+last)/2 mergesort(list, first, mid) mergesort(list, mid+1, last) merge(list, first, mid, last) return Exercise: Trace Mergesort Execution Can you trace MergeSort() on this list? A = {8, 3, 2, 9, 7, 1, 5, 4}; Efficiency of Mergesort Cost to divide in half? No comparisons Two subproblems: each size n/2 Combining results? What is the cost of merging two lists of size n/2 Soon well see its n1 in the worstcase Recurrence relation: W(1) = 0 W(n) = 2 W(n/2)+ Wmerge(n) = 2 W(n/2) + n1 You can now show that this W(n) (n log n) Merging Sorted Sequences Problem: Given two sequences A and B sorted in non...
View
Full
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.
 Spring '08
 BLOOMFIELD,A
 Algorithms, Computer Graphics, Sort

Click to edit the document details