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

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
Garfield AP Computer Science
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Chapter 8, Sorting. Sorting, Ordering, or Sequencing “Since only two of our tape drives were in working order, I was ordered to order more tape units.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
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
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Computation for Physics 計算物理概論 Algorithm. An algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing,
Design Patterns for Object Oriented systems CSC 515 Ashwin Dandwate.
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?
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Quicksort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
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 ( ) ( ) ( )
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
(c) , University of Washington
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
Data Structure Dr. Mohamed Khafagy. Abstraction.Abstraction is a technique in which we construct a model of an entity based upon its essential Characteristics.
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
OOP in Introductory CS Stephen Wong and “Zung” Nguyen Rice University Better students though abstraction.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Design Patterns for Marine Biology Simulation Dung “Zung” Nguyen Mathias Ricken Stephen Wong Rice University.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Nifty Assignments: Marine Biology Simulation OOPSLA ‘04 Educators’ Symposium October 25, 2004 Eric Cheng Mathias Ricken Dung “Zung” Nguyen Stephen Wong.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Sort Algorithms.
Some comments on lab4. Hi Philippe! Can you tell me if my code works? Thanks! I’ll show you what works…
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
Today’s Material Sorting: Definitions Basic Sorting Algorithms
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Help Session 3: Induction and Complexity Ezgi, Shenqi, Bran.
Design Patterns: MORE Examples
Design Patterns for Sorting Teaching something old in a new light
Software Design and Architecture
Department of Computer Science
Design Patterns for Sorting something old in a new light
Programming in Java: lecture 10
Design Patterns for Sorting Teaching something old in a new light
Advanced Sorting Methods: Shellsort
Design Patterns For Sorting
Nifty Assignments: Marine Biology Simulation
Marine Biology Simulation Part III: Assignment, Milestone 2
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant.
CS 325: Software Engineering
Marine Biology Simulation Part II: Assignment, Milestone 1
Chapter 8, Design Patterns Factory
Presentation transcript:

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

Thanks! Computer Science by Stan Warford, Pepperdine University

What is Sorting Anyway? Can We Abstract All Sorting Processes? Some concrete examples:  Selection sort Selection sort  Insertion sort Insertion sort

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

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

Abstract Sorter Class 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); } Concrete “Template Method” SortAlgoSelectionInsertion abstract, relegated to subclasses

Template Method Pattern AClass + invariant(…); # variant1(…); # variant2(…); invariant () { … variant1 (…); … variant2 (…); … } ConcreteX # variant1(…); # variant2(…); ConcreteY # variant1(…); # variant2(…);  Expresses invariant in terms of variants.  White-box Framework:  Extension by subclassing

Sort Framework 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); } } // A[lo:s-1], A[s:hi] form a proper partition of A[lo:hi]. // A[lo:s-1] is sorted. // A[s:hi] is sorted. // A[lo:hi] is sorted. Recursive case “Free” Focus points Base case is trivial // else if (hi <= lo) do nothing!

Insertion Sort  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] }  Reduces to insertion of a single object into a sorted array.  Simplifies proof of correctness.

Selection Sort  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) { }  Reduces to selecting a minimum value in the array.  Simplifies proof of correctness Do Nothing!

Time Complexity  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C  S(l, h)  T(l, s-1)  T(s, h)  J(l, s, h) 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

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  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 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) O(n), n = h - l O(1)  Let n = h – l, T(l, h) = T(n) = C if n < 1 T(n-1) + O(n) = O(n 2 ) if 1 <= n

Sorting as a Framework  Unifies sorting under one foundational principle: Divide and Conquer!  Reduces code complexity. Increases robustness.  Simplifies program verification and complexity analysis. 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);}

MergeInsertion Classifications HeapSortSelectionQuickSortBubble 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); Easy split/Hard join Hard split/Easy join

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

Extending Without Changing u Graphics, sort order and performance measurements are completely separate from the sorting. u Add functionality to the sorters, ordering operators, and sortable objects without disturbing their abstract behavior. u Wrap the sorters, operators and objects in something abstractly equivalent that adds functionality: Decorators The Abstract is the Invariant

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

Sorters Abstract Template Pattern sorter Sort using an abstract ordering strategy Concrete sorters implement split() & join() Decorated ASorter for graphical split & join Abstract objects being sorted “Decoratable”! ASorter uses the ordering strategy for comparisons “Decoratable”!

GraphicSorter Decorator 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 } Decoree. Delegation to the decoree. Graphics decoration. Delegation to the decoree. Graphics decoration. Identical behavior as the decoree.

Abstract comparison operator Decorated AOrder to count comparisons Decorated AOrder to graphically show comparisons Decorated AOrder to reverse sort order Comparison Strategies

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

Teaching beyond sorting… Abstraction unifies and simplifiesInstead of disparate and complex Abstraction enables flexibility and extensibilityInstead of rigidity and limitedness Design Patterns Express Abstraction Change the way the students think!

Download the paper!