Design Patterns for Sorting Teaching something old in a new light

Slides:



Advertisements
Similar presentations
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Advertisements

Introduction to Algorithms Quicksort
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Design Patterns for Marine Biology Simulation Dung “Zung” Nguyen Mathias Ricken Stephen Wong Rice University.
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
Sorting Algorithms and Average Case Time Complexity
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
CMPS1371 Introduction to Computing for Engineers SORTING.
CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.
CS 280 Data Structures Professor John Peterson. Project Questions?
CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
OOP in Introductory CS Stephen Wong and “Zung” Nguyen Rice University Better students though abstraction.
Design Patterns for Sorting Teaching something old in a new light Dung “Zung” Nguyen Stephen Wong Rice University.
Design Patterns for Marine Biology Simulation Dung “Zung” Nguyen Mathias Ricken Stephen Wong Rice University.
Sorting CS 105 See Chapter 14 of Horstmann text. Sorting Slide 2 The Sorting problem Input: a collection S of n elements that can be ordered Output: the.
Sort Algorithms.
Merge Sort. Stable vs. Non-Stable Sorts We frequently use sorting methods for items with multiple keys Sometimes we need to apply the sorting with different.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Patterns for Decoupling Data Structures and Algorithms or How visitors can help you grow! Stephen Wong, Oberlin College Dung “Zung” Nguyen, Pepperdine.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Help Session 3: Induction and Complexity Ezgi, Shenqi, Bran.
Recursion Powerful Tool
Prof. U V THETE Dept. of Computer Science YMA
Chapter 0: Introduction
Design Patterns: MORE Examples
Introduction to .NET Florin Olariu
Unit II-Chapter No. : 5- design Patterns
Design Patterns Lecture part 2.
Decrease-and-Conquer Approach
Design Patterns for Sorting Teaching something old in a new light
COMP 53 – Week Seven Big O Sorting.
Design Patterns.
Simple Sorting Algorithms
Department of Computer Science
Design Patterns for Sorting something old in a new light
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
Chapter 7 Sorting Spring 14
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Advanced Sorting Methods: Shellsort
Design Patterns For Sorting
CS 3343: Analysis of Algorithms
Ch 7: Quicksort Ming-Te Chi
Nifty Assignments: Marine Biology Simulation
Yan Shi CS/SE 2630 Lecture Notes
Sub-Quadratic Sorting Algorithms
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
Sorting Chapter 8.
Simple Sorting Algorithms
CSC 143 Java Sorting.
CS 325: Software Engineering
Marine Biology Simulation Part II: Assignment, Milestone 1
Searching and Sorting Hint at Asymptotic Complexity
Searching/Sorting/Searching
Chapter 8, Design Patterns Introduction
Searching and Sorting Hint at Asymptotic Complexity
Presented by Igor Ivković
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 8, Design Patterns Factory
Simple Sorting Algorithms
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

Design Patterns for Sorting Teaching something old in a new light Dung “Zung” Nguyen, Rice University Stephen Wong, Oberlin College

* Computer Science by Stan Warford, Pepperdine University 07/16/96 Thanks! Computer Science by Stan Warford, Pepperdine University Not a plug for Stan’s book because nobody in the right mind would buy a Pascal textbook these days. For showing us the way… Show demo after this. *

What is Sorting Anyway? Can We Abstract All Sorting Processes? * 07/16/96 What is Sorting Anyway? Can We Abstract All Sorting Processes? Some concrete examples: Selection sort Insertion sort More than just a bunch of different loops How can we express what is going on in a cohesive way? *

Merritt’s Thesis for Sorting All comparison-based sorting can be viewed as “Divide and Conquer” algorithms. Sort a pile Split the pile into smaller piles Sort each the smaller piles Join the sorted smaller piles into sorted pile

Hypothetical Sort Divide and Conquer! Unsorted Sorted split join = unsorted = sorted = sort process Hypothetical Sort Divide and Conquer! How can we capture this abstraction?

Abstract Sorter Class if (lo < hi) { int s = split (A, lo, hi); * 07/16/96 if (lo < hi) { int s = split (A, lo, hi); sort (A, lo, s-1); sort (A, s, hi); join (A, lo, s, hi); } Abstract Sorter Class Concrete “Template Method” ASorter + void: sort(Object[ ] A, int: lo, int: hi); SortAlgo Selection Insertion abstract, relegated to subclasses # int: split(Object[] A, int lo, int hi); Explain UML diagram -- industry standard. # void: join(Object[] A, int lo, int s, int hi); *

Template Method Pattern * invariant () { … variant1 (…); variant2 (…); } 07/16/96 Template Method Pattern AClass + invariant(…); # variant1(…); # variant2(…); Expresses invariant in terms of variants. White-box Framework: Extension by subclassing ConcreteX # variant1(…); # variant2(…); ConcreteY The variant behavior is a service to the framework. *

// else if (hi <= lo) do nothing! * Sort Framework 07/16/96 void sort (Object A[ ], int lo, int hi) { if (lo < hi) { int s = split (A, lo, hi); sort (A, lo, s-1); sort (A, s, hi); join (A, lo, s, hi); } } Recursive case // A[lo:s-1], A[s:hi] form a proper partition of A[lo:hi]. Focus points // A[lo:s-1] is sorted. “Free” // A[s:hi] is sorted. What does the framework buy us? Reduced code complexity and as a result simplicity! Need only focus on making sure that split and join establish their respective post-conditions. Proper partitions = both non-empty // A[lo:hi] is sorted. Base case is trivial // else if (hi <= lo) do nothing! *

* 07/16/96 Insertion Sort Reduces to insertion of a single object into a sorted array. Simplifies proof of correctness. int split(Object[] A, int lo, int hi) { return hi; // A splits into A[lo:hi-1] and A[hi:hi] } void join(Object[] A, int lo, int s, int hi) { // Pre: A[lo:hi-1] is sorted, s = hi. Object key = A[hi]; int j; for (j = hi; lo < j && aOrder.lt(key, A[j-1]); j- -) A[j] = A[j-1]; A[j] = key; // Post: A[hi] is inserted in order into A[lo:hi-1] } lo < hi strictly s<= hi Framework enforces correctness. aOrder is an ordering strategy to be discussed by Stephen later. Students are able to experience a framework system on a small but practical scale. *

Selection Sort Reduces to selecting a minimum value in the array. * 07/16/96 Selection Sort Reduces to selecting a minimum value in the array. Simplifies proof of correctness int split(Object[] A, int lo, int hi) { int s = lo; for (int i= lo+1; i <= hi; I++) { if (aOrder.lt(A[i], A[s])) s = i; } // s = index of min value swap (A, lo, s); // A[lo] = min value in A[lo:hi] return lo + 1; // A splits into A[lo:lo] and A[lo+1:hi] } void join(Object[] A, int lo, int s, int hi) { } Do Nothing! *

Time Complexity void sort (Object A[ ], int l, int h) { * 07/16/96 void sort (Object A[ ], int l, int h) { if (l < h) { int s = split (A, l, h); sort (A, l, s-1); sort (A, s, h); join (A, l, s, h);}} T(l, h) C S(l, h) T(l, s-1) T(s, h) J(l, s, h) Recurance relation T(l, h) = C if h <= l C + S(l, h) + T(l, s-1) + T(s, h) + J(l, s, h) if l < h *

Insertion Sort Complexity int split (Object[ ] A, int l, int h) { return h; } // O(1) void join (Object[ ] A, int l, int s, int h) { Object key = A[h]; int j; for (j = h; l < j && aOrder.lt(key, A[j-1]); j- -) A[j] = A[j-1]; A[j] = key; } // O(h-l) T(l, h) = C if h <= l C + S(l, h) + T(l, s-1) + T(s, h) + J(l, s, h) if l < h s = h O(n), n = h - l T(l, h) = C if h <= l C + S(l, h) + T(l, h-1) + T(h, h) + J(l, h, h) if l < h O(1) Let n = h – l, T(l, h) = T(n) = C if n < 1 T(n-1) + O(n) = O(n2) if 1 <= n

Sorting as a Framework if (lo < hi) { int s = split (A, lo, hi); ASorter + void: sort(Object[ ] A, int: lo, int: hi); # int: split(Object[] A, int lo, int hi); # void: join(Object[] A, int lo, int s, int hi); if (lo < hi) { int s = split (A, lo, hi); sort (A, lo, s-1); sort (A, s, hi); join (A, lo, s, hi);} Sorting as a Framework Unifies sorting under one foundational principle: Divide and Conquer! Reduces code complexity. Increases robustness. Simplifies program verification and complexity analysis.

Classifications Easy split/Hard join Merge Insertion ASorter HeapSort + void: sort(Object[] a, int: lo, int: hi); # int: split(Object[] A, int lo, int hi); # void: join(Object[] A, int lo, int s, int hi); HeapSort Selection QuickSort Bubble Hard split/Easy join

It’s all about abstraction… It’s more than just sorting… Not Just Buzzwords… It’s more than just sorting… Abstraction teaches software engineering Reusability: write once/use many Reuse the invariant: the framework Flexibility: change the variants Add new sorters Change sort order Extensibility: add new capabilities Visualization Performance measurements

Extending Without Changing * 07/16/96 Extending Without Changing The Abstract is the Invariant Graphics, sort order and performance measurements are completely separate from the sorting. Add functionality to the sorters, ordering operators, and sortable objects without disturbing their abstract behavior. Wrap the sorters, operators and objects in something abstractly equivalent that adds functionality: Decorators Run demo again Emphasize on separation of sorting and animation. Visualization helps student understand the “picture” of sorting, but what we want to teach here is software engineering as well. *

Decorator Design Pattern Subclass holds an instance of its superclass Decorator intercepts calls to decoree Client AComponent uses + Object: method(Object x) 1 Client deals with an abstract entity ConcreteImpl + Object: method(Object x) Decorator + Object: method(Object x) // do additional processing Object y = decoree.method(x); // do more processing return y; - AComponent: decoree Decorator is abstractly equivalent to the decoree Client doesn’t know the decoration exists! Decorators can be layered on top of each other Decorator performs additional processing

Abstract Template Pattern sorter * 07/16/96 Sorters Abstract Template Pattern sorter Concrete sorters implement split() & join() ASorter uses the ordering strategy for comparisons “Decoratable”! Abstract objects being sorted Sort using an abstract ordering strategy “Decoratable”! Discuss strategy pattern also. Decorate sorter and strategy because we have the abstract class Decorated ASorter for graphical split & join *

GraphicSorter Decorator Decoree. private ASorter sorter; int split(Object[] A, int lo, int hi) { int s = sorter.split(A, lo, hi); // recolor split sections and pause return s; } void join(Object[] A, int lo, int s, int hi) { sorter.join(A, lo, s, hi); // recolor joined sections and pause Delegation to the decoree. Graphics decoration. Identical behavior as the decoree. Delegation to the decoree. Graphics decoration.

Comparison Strategies * 07/16/96 Comparison Strategies Abstract comparison operator Decorated AOrder to graphically show comparisons Decorated AOrder to count comparisons Decorated AOrder to reverse sort order STRATEGY PATTERN Show demo program to point out: Graphical comparison Performance measurements Reverse sort order *

Sortable Integers Abstract class Factory method for comparison strategy Concrete implementation Decorated AInteger to count accesses IColoredObject adapter + AInteger decorator for graphics

Teaching beyond sorting… * 07/16/96 Design Patterns Express Abstraction Instead of disparate and complex Abstraction unifies and simplifies Abstraction enables flexibility and extensibility Instead of rigidity and limitedness Change the way the students think! Instead of an amalgam of disparate and complex concrete algorithms, abstraction creates a unified and simplified whole. Instead of a rigid and limited set of tools, abstraction enables as a flexible and extensible system of cooperating components. *

Download the paper! http://exciton.cs.oberlin.edu/research/SIGCSE01