Design Patterns for Sorting Teaching something old in a new light - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Design Patterns for Sorting Teaching something old in a new light

Description:

Abstraction unifies and simplifies. Instead of disparate and complex. Abstraction enables flexibility and extensibility. Instead of rigidity and limitedness ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 24
Provided by: stephenwon
Category:

less

Transcript and Presenter's Notes

Title: Design Patterns for Sorting Teaching something old in a new light


1
Design Patterns for SortingTeaching something
old in a new light
  • Dung Zung Nguyen, Rice University
  • Stephen Wong, Oberlin College

2
Thanks!
  • Computer Science
  • by Stan Warford, Pepperdine University

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

4
Merritts 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

5
Hypothetical Sort
  • Divide and Conquer!
  • How can we capture this abstraction?

6
Abstract Sorter Class
Concrete Template Method
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)
7
Template Method Pattern
  • Expresses invariant in terms of variants.
  • White-box Framework
  • Extension by subclassing

8
Sort Framework
  • void sort (Object A , int lo, int hi) if
    (lo lt hi) int s split (A, lo, hi)
    sort (A, lo, s-1) sort (A, s, hi)
    join (A, lo, s, hi)

Recursive case
// Alos-1, Ashi form a proper partition of
Alohi.
// Alos-1 is sorted.
// Ashi is sorted.
// Alohi is sorted.
9
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 Alohi-1 and
    Ahihi
  • void join(Object A, int lo, int s, int hi)
  • // Pre Alohi-1 is sorted, s hi.
  • Object key Ahiint jfor (j hi lo lt j
    aOrder.lt(key, Aj-1) j- -) Aj
    Aj-1Aj key// Post Ahi is inserted in
    order into Alohi-1

10
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 lofor (int i lo1 i lt hi I)
    if (aOrder.lt(Ai, As)) s i
  • // s index of min value
  • swap (A, lo, s)
  • // Alo min value in Alohi
  • return lo 1
  • // A splits into Alolo and Alo1hi
  • void join(Object A, int lo, int s, int hi)

Do Nothing!
11
Time Complexity
  • void sort (Object A , int l, int h)
  • if (l lt 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)
  • T(l, h)
  • C if h lt l
  • C S(l, h) T(l, s-1) T(s, h) J(l, s, h)
    if l lt h

12
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 Ah int j for (j h l lt j
    aOrder.lt(key, Aj-1) j- -) Aj
    Aj-1 Aj key // O(h-l)

O(n), n h - l
  • T(l, h)
  • C if h lt l
  • C S(l, h) T(l, h-1) T(h, h) J(l, h, h)
    if l lt h

O(1)
  • Let n h l, T(l, h) T(n)
  • C if n lt 1
  • T(n-1) O(n) O(n2) if 1 lt n

13
Sorting as a Framework
  • Unifies sorting under one foundational
    principle Divide and Conquer!
  • Reduces code complexity. Increases robustness.
  • Simplifies program verification and complexity
    analysis.

14
Classifications
Easy split/Hard join
Hard split/Easy join
15
Not Just Buzzwords
Its more than just sorting
Its all about abstraction
  • 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

Abstraction teaches software engineering
16
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

17
Decorator Design Pattern
Subclass holds an instance of its superclass
Decorator intercepts calls to decoree
uses
Object method(Object x)
Client deals with an abstract entity
- AComponent decoree
Decorator performs additional processing
Decorators can be layered on top of each other
Decorator is abstractly equivalent to the decoree
Client doesnt know the decoration exists!
18
Sorters
Abstract TemplatePatternsorter
ASorter uses the ordering strategy for comparisons
Decoratable!
Sort using anabstract orderingstrategy
Abstract objectsbeing sorted
Decoratable!
DecoratedASorterfor graphicalsplit join
19
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 tothe decoree.
Graphicsdecoration.
Identical behavioras the decoree.
Delegation tothe decoree.
Graphicsdecoration.
20
Comparison Strategies
Abstract comparisonoperator
Decorated AOrder to graphicallyshow comparisons
Decorated AOrder to countcomparisons
Decorated AOrder to reversesort order
21
Sortable Integers
Abstract class
Factorymethodforcomparison strategy
Concreteimplementation
Decorated AIntegerto countaccesses
IColoredObject adapter AInteger decoratorfor
graphics
22
Teaching beyond sorting
Design Patterns Express Abstraction
Abstraction unifies and simplifies
Instead of disparate and complex
Abstraction enables flexibility and extensibility
Instead of rigidity and limitedness
Change the way the students think!
23
Download the paper!http//exciton.cs.oberlin.edu
/research/SIGCSE01
Write a Comment
User Comments (0)
About PowerShow.com