Design Patterns For Sorting Lauren Schmidt
Knuth Three Sorting Taxonomies Insertion Items inserted right where they need to go Selection Smallest located first, then next item, etc Exchange Items swapped when found out of order Algorithms based on how items are chosen More complicated algorithms try to optimize these three
Recursive notion of sorting Top-down approach Merritt’s Thesis All comparison based sorting can be seen as a “Divide and Conquer” Algorithm split into two groups: Hard split/easy join – Merge sort Easy split/hard join – Quick sort Recursive notion of sorting Top-down approach
SORTING WITH MERRITT’s Thesis Start with an unsorted pile (blue) Red dashes represent the “black box” where sorting happens After going into the black box, the pile is sorted (yellow)
SPLIT Unsorted pile is split into multiple unsorted piles
SORT Smaller unsorted piles can continue to be split and sorted Base case reached when single element in pile
JOIN Sorted piles are then joined into a single sorted pile
Why do we care? We can describe using OO concepts! Which sorting processes are invariant? Split into subpiles Sort subpiles Join subpiles Which sorting processes are variant? Algorithm to split Algorithm to sort Algorithm to join
Some constraints Sorting arrays in place Given a comparator to compare objects in array
Design patterns for sorting Template method pattern Captures abstract sort procedure Strategy pattern Decouple sort from comparison Decorator design pattern Makes design flexible/extensible
Union design pattern to capture invariant processes Template method Union design pattern to capture invariant processes Split Join Template design pattern to capture invariant sort process
Strategy pattern for object comparison Sorting and comparing objects decoupled from one another ASorter maintains reference to Aorder Reuse of same sorting strategies and/or ordering schemes for different algorithms
Decorator pattern for analysis Add on performance analysis without changing previously described architecture
Easier to analyze complexity of more complicated sorting algorithms What do we gain? Extensibility – can easily piece together join and sort strategies to construct different sorting algorithms Easier to analyze complexity of more complicated sorting algorithms Wong & Dung suggest for educational purposes OO Design introduction + sorting algorithms!
What do we lose? Overhead causes runtime performance to take a hit
SOurces Connextions Module: Design Patterns for Sorting Nguyen, Wong. Design Patterns for Sorting (2001). Merritt, S. An Inverted Taxonomy of Sorting Algorithms, Comm. ACM, 28, 1 (Jan. 1985), 96-99.