43
Remember Sorting_Machine? Isn’t it a beauty! Go ahead . . . kick the tires! Change_To_ Extraction_Phase In Out

Remember Sorting_Machine?

  • Upload
    makara

  • View
    36

  • Download
    5

Embed Size (px)

DESCRIPTION

Change_To_ Extraction_Phase. In. Out. Remember Sorting_Machine?. Isn’t it a beauty! Go ahead . . . kick the tires!. 2: Push the button. 1: Items go in here. 3: Sorted items come out here. Sorting_Machine Continued…. Change_To_ Extraction_Phase. In. Out. - PowerPoint PPT Presentation

Citation preview

Page 1: Remember Sorting_Machine?

Remember Sorting_Machine?

Isn’t it a beauty!Go ahead . . .kick the tires!

Change_To_Extraction_Phase

In Out

Page 2: Remember Sorting_Machine?

Sorting_Machine Continued…

Change_To_Extraction_Phase

In Out

3: Sorted items come out here

2: Push the button

1: Itemsgo in here

Page 3: Remember Sorting_Machine?

Sorting_Machine Continued…

Type (

inserting: boolean contents: multiset of Item)

Initial value (true, {})

Page 4: Remember Sorting_Machine?

A Math Type: Multiset Multisets are just like sets except

that duplicates are allowed In set theory:

{2, 18, 2, 36} = {2, 18, 36} and|{2, 18, 2, 36}| = 3

In multiset theory:{2, 18, 2, 36} /= {2, 18, 36} and|{2, 18, 2, 36}| = 4

Page 5: Remember Sorting_Machine?

Sorting_Machine Continued…

Operations m.Insert (x) m.Change_To_Extraction_Phase ( ) m.Remove_First (x) m.Remove_Any (x) m.Is_In_Extraction_Phase ( ) m.Size ( )

Page 6: Remember Sorting_Machine?

Sorting With Sorting_Machine// input a bunch of integers placing them in sorting machine mwhile (not ins.At_EOS ()){ object Integer i; ins >> i; m.Insert (i);}// now output the same integers in sorted orderm.Change_To_Extraction_Phase ();while (m.Size () > 0){ object Integer i; m.Remove_First (i); outs << i << ‘\n’;}

Page 7: Remember Sorting_Machine?

Sorting Algorithms

Selection Sort Insertion Sort Mergesort Quicksort Heapsort Tree Sort …

Page 8: Remember Sorting_Machine?

A Sort Procedure

procedure Sort ( alters Queue_Of_Item& q ); /*! ensures q is permutation of #q and IS_ORDERED (q) !*/

Page 9: Remember Sorting_Machine?

Math Definition

math definition IS_ORDERED ( s: string of Item ): boolean is for all u, v: Item where (<u> * <v> is substring

of s) (ARE_IN_ORDER (u, v))

Page 10: Remember Sorting_Machine?

An Old Math Definition

math definition ARE_IN_ORDER ( x: Item, y: Item ): boolean satisfies restriction for all x, y, z: Item (ARE_IN_ORDER (x, x) and (ARE_IN_ORDER (x, y) or ARE_IN_ORDER (y, x)) and (if (ARE_IN_ORDER (x, y) and ARE_IN_ORDER (y, z)) then ARE_IN_ORDER (x, z)))

Page 11: Remember Sorting_Machine?

Selection Sortprocedure Remove_Min ( alters Queue_Of_Item& q, produces Item& x ); /*! requires q /= empty_string ensures (q * <x>) is permutation of #q and for all y: Item where (y is in elements (q)) (ARE_IN_ORDER (x, y)) !*/

Page 12: Remember Sorting_Machine?

Selection Sort: You Give It A Tryprocedure Sort ( alters Queue_Of_Item& q ){

}

object Queue_Of_Item tmp; while (q.Length () > 0) { object Item x; Remove_Min (q, x); tmp.Enqueue (x); } q &= tmp;

Page 13: Remember Sorting_Machine?

Insertion Sortprocedure Insert_In_Order ( alters Queue_Of_Item& q, consumes Item& x ); /*! requires IS_ORDERED (q) ensures q is permutation of (#q * <#x>) and IS_ORDERED (q) !*/

Page 14: Remember Sorting_Machine?

Insertion Sort: You Give It A Try

procedure Sort ( alters Queue_Of_Item& q ){

}

object Queue_Of_Item tmp; while (q.Length () > 0) { object Item x; q.Dequeue (x); Insert_In_Order (tmp, x); } q &= tmp;

Page 15: Remember Sorting_Machine?

Quicksortprocedure Partition ( consumes Queue_Of_Item& q, preserves Item& p, produces Queue_Of_Item& q1, produces Queue_Of_Item& q2 ); /*! ensures q1 * q2 is permutation of #q and for all x: Item where (x is in elements (q1)) (ARE_IN_ORDER (x, p)) and for all x: Item where (x is in elements (q2)) (not ARE_IN_ORDER (x, p)) !*/

Page 16: Remember Sorting_Machine?

Quicksort Continued…procedure Combine ( produces Queue_Of_Item& q, consumes Item& p, consumes Queue_Of_Item& q1, consumes Queue_Of_Item& q2 ); /*! ensures q = #q1 * <#p> * #q2 !*/

Page 17: Remember Sorting_Machine?

Quicksort: You Give It A Tryprocedure Sort ( alters Queue_Of_Item& q ){

}

if (q.Length () > 1) { object Item x; object Queue_Of_Item q1, q2; q.Dequeue (x); Partition (q, x, q1, q2); Sort (q1); Sort (q2); Combine (q, x, q1, q2); }

Page 18: Remember Sorting_Machine?

Quicksort Continued…procedure Partition ( consumes Queue_Of_Item& q, preserves Item& p, produces Queue_Of_Item& q1, produces Queue_Of_Item& q2 ){

}

q1.Clear (); q2.Clear (); while (q.Length () > 0) { object Item x; q.Dequeue (x); if (Item_Are_In_Order::Are_In_Order (x, p)) { q1.Enqueue (x); } else { q2.Enqueue (x); } }

Page 19: Remember Sorting_Machine?

Quicksort Continued…procedure Combine ( produces Queue_Of_Item& q, consumes Item& p, consumes Queue_Of_Item& q1, consumes Queue_Of_Item& q2 ){

}

q.Clear (); q &= q1; q.Enqueue (p); while (q2.Length () > 0) { object Item x; q2.Dequeue (x); q.Enqueue (x); }

Page 20: Remember Sorting_Machine?

Mergesortprocedure Split ( consumes Queue_Of_Item& q, produces Queue_Of_Item& q1, produces Queue_Of_Item& q2 ); /*! ensures q1 * q2 is permutation of #q and |q2| <= |q1| <= |q2| + 1 !*/

Page 21: Remember Sorting_Machine?

Mergesort Continued…procedure Merge ( consumes Queue_Of_Item& q1, consumes Queue_Of_Item& q2, produces Queue_Of_Item& q ); /*! requires IS_ORDERED (q1) and IS_ORDERED (q2) ensures q is permutation of #q1 * #q2 and IS_ORDERED (q) !*/

Page 22: Remember Sorting_Machine?

Mergesort: You Give It A Try

procedure Sort ( alters Queue_Of_Item& q ){

}

if (q.Length () > 1) { object Queue_Of_Item q1, q2; Split (q, q1, q2); Sort (q1); Sort (q2); Merge (q1, q2, q); }

Page 23: Remember Sorting_Machine?

Sorting_Machine: Inside Story

Are you ready tolook under thehood of this baby?

Change_To_Extraction_Phase

In Out

Page 24: Remember Sorting_Machine?

Inside Story Continued…

As an example, assume the representation for Sorting_Machine has two fields: contents_rep of type Queue_Of_Item inserting_rep of type Boolean

What are some possible implementations?

Page 25: Remember Sorting_Machine?

Let’s Procrastinate — the Students’ Choice

operation

m.Insert (x)

m.Change_To_ Extraction_Phase ()

m.Remove_First (x)

m.Remove_Any (x)

what happens?

q.Enqueue (x)

set phase to extraction

Remove_Min (q, x)

q.Dequeue (x)

c1

c2

c3n

c4

How long will it take to perform each operationas a function of n = |m.contents|?

cn2

c1n

c3n2

Page 26: Remember Sorting_Machine?

How About Eager Beavers

operation

m.Insert (x)

m.Change_To_ Extraction_Phase ()

m.Remove_First (x)

m.Remove_Any (x)

what happens?

Insert_In_Order (q, x)

set phase to extraction

q.Dequeue (x)

q.Dequeue (x)

c1n

c2

c3

c3

How long will it take to perform each operationas a function of n = |m.contents|?

cn2

c1n2

c3n

Page 27: Remember Sorting_Machine?

Are There Other Possibilities?

operation

m.Insert (x)

m.Change_To_ Extraction_Phase ()

m.Remove_First (x)

m.Remove_Any (x)

what happens?

q.Enqueue (x)

q.Sort ()set phase to extraction

q.Dequeue (x)

q.Dequeue (x)

c1

c2nlogn

c3

c3

How long will it take to perform each operationas a function of n = |m.contents|?

cnlogn

c1n

c3n

Page 28: Remember Sorting_Machine?

A Heapsort Implementation

An ARE_IN_ORDER Heap is a special kind of binary tree: shape property: the tree is complete ordering property: for each item x in

the tree, ARE_IN_ORDER (x, child) holds for each child of x

Page 29: Remember Sorting_Machine?

Complete Binary Trees All levels of the tree are completely

filled up except possibly the bottom level. Any “holes” in the bottom level must appear to the right of all existing items at that level.

Page 30: Remember Sorting_Machine?

Heap Ordering Property For each item x in the tree:

x

y z

ARE_IN_ORDER (x, y) andARE_IN_ORDER (x, z)

x

yARE_IN_ORDER (x, y)

Page 31: Remember Sorting_Machine?

Examples

18

36 24

5853 61

18

36 24

53 12 18

36 24

53 61 58

14

21 12

Item is IntegerARE_IN_ORDER (x, y) x y

18

36

53 38

14

21

Page 32: Remember Sorting_Machine?

Heapsort

Build an ARE_IN_ORDER heap with the items to be sorted using the specified ARE_IN_ORDER

Implement Remove_First so that, after removing the first item in the ordering from the heap, it restores the heap properties

Page 33: Remember Sorting_Machine?

How Will Remove_First Work?

12

46 18

54 73 37

82 63

99

46 18

54 73 37

82 63

99

1263

46 18

54 73 37

82

99

18

46 63

54 73 37

82

99

18

46 37

54 73 63

82

99

Page 34: Remember Sorting_Machine?

Sift _Root _Down

procedure Sift_Root_Down ( alters “binary tree” t ); /*! requires [t is a complete tree and both left and right subtrees of t are heaps] ensures [t is a heap and t contains exactly the items in #t] !*/

Page 35: Remember Sorting_Machine?

Turning a Complete Tree Into a Heap

procedure Heapify ( alters “binary tree” t ); /*! requires [t is a complete tree] ensures [t is a heap and t contains exactly the items in #t] !*/

Hint:

Page 36: Remember Sorting_Machine?

Turning a Complete Tree Into a Heap Continued…

procedure_body Heapify ( alters “binary tree” t ){

}

if (|t| > 1) { Heapify (left subtree of t) Heapify (right subtree of t) Sift _Root_Down (t) }

Page 37: Remember Sorting_Machine?

Mapping Complete Binary TreePositions Into Array Locations

12

46 18

54 73 37

82 63

99

Let’s number the tree positions (top-bottom, left-right)(1)

(2) (3)

(4) (5) (6) (7)

(9)(8)

Which array corresponds to the tree?

1 2 3 4 5 6 7 8 9

12 46 18 54 73 37 99 82 63

x

y z

(i)

(2i) (2i+1)

Page 38: Remember Sorting_Machine?

Remember Array Operations?

a.Set_Bounds (lower, upper) a[i] --accessor operation a.Lower_Bound () a.Upper_Bound ()

Page 39: Remember Sorting_Machine?

Insertion-Phase Container?

What is the effect ofa.Set_Bounds (lower, upper)?

At what point will the Sorting_Machine be ready to set the array bounds?

Page 40: Remember Sorting_Machine?

Swapping/Comparing Two Elements in an Array Given:

object Array_Of_Item a;object Integer i, j;

What’s wrong with: a[i] &= a[j] and Item_Are_In_Order::Are_In_Order (a[i], a[j])?

They violate the repeated argument rule a[i] and a[j] are references to parts of a’s

representation: the parts could be the same (see Partial_Map_Kernel_3)

Page 41: Remember Sorting_Machine?

Swapping Two Array Elements Use a.Exchange_At (i, j) instead of

a[i] &= a[j] Exchange_At is an Array extension It requires that a.lb i, j a.ub See AT/Array/Exchange_At.h in the

RESOLVE_Catalog for complete specs

Page 42: Remember Sorting_Machine?

Comparing Two Array Elements Use a.Are_In_Order_At (i, j) instead of

Item_Are_In_Order::Are_In_Order (a[i], a[j])

Are_In_Order_At is an Array extension It requires that a.lb i, j a.ub See AT/Array/Are_In_Order_At.h in the

RESOLVE_Catalog for complete specs

Page 43: Remember Sorting_Machine?

Sorting_Machine_Kernel_2: Component Coupling Diagram

Sorting_Machine_Type

ext

i

Sorting_Machine_Kernel

Sorting_Machine_Kernel_2

Representation Array_Kernel

u uenc

i i i

Queue_Kernel

Array_Exchange_At

Array_Are_In_Order_At

General_Are_In_Order

i u

i

i