sortingmachine
play

SortingMachine 8 February 2019 OSU CSE 1 SortingMachine The - PowerPoint PPT Presentation

SortingMachine 8 February 2019 OSU CSE 1 SortingMachine The SortingMachine component family allows you to add elements of type T to a collection of such elements, and then to remove them one at a time in sorted order according to a


  1. SortingMachine 8 February 2019 OSU CSE 1

  2. SortingMachine • The SortingMachine component family allows you to add elements of type T to a collection of such elements, and then to remove them one at a time in sorted order according to a client-supplied ordering – Queue and Stack support removal in FIFO and LIFO order, respectively 8 February 2019 OSU CSE 2

  3. SortingMachine FIFO and LIFO are • The SortingMachine component family time-based orderings; a allows you to add elements of type T to a SortingMachine uses a value-based ordering. collection of such elements, and then to remove them one at a time in sorted order according to a client-supplied ordering – Queue and Stack support removal in FIFO and LIFO order, respectively 8 February 2019 OSU CSE 3

  4. Why Not Use The sort Method? while (/*input values remain*/) { /* let x = next input value */ q.enqueue(x); } q.sort(order); while (q.length() > 0) { T x = q.dequeue(); /* output x */ } 8 February 2019 OSU CSE 4

  5. Why Not Use The sort Method? while (/*input values remain*/) { /* let x = next input value */ q.enqueue(x); The Java libraries have similar methods to sort } built-in arrays and other q.sort(order); “collections”. while (q.length() > 0) { T x = q.dequeue(); /* output x */ } 8 February 2019 OSU CSE 5

  6. Example • Suppose you want to find the students with the 50 highest GPAs among all Ohio State students – Modify the previous code to show how you might do this ... 8 February 2019 OSU CSE 6

  7. Performance in the Example • Code using a sort method spends time to sort all 50,000 GPAs just to find the top 50 • Code based on SortingMachine can be much more efficient, because if you don’t remove all of the elements you don’t necessarily pay for sorting all of them 8 February 2019 OSU CSE 7

  8. Interfaces and Classes Standard Iterable extends extends SortingMachine- Kernel extends SortingMachine implements implements ... SortingMachine1L SortingMachine5 8 February 2019 OSU CSE 8

  9. Interfaces and Classes Standard Iterable extends extends SortingMachine- Kernel SortingMachineKernel extends has contracts for six methods: add SortingMachine changeToExtractionMode removeFirst implements implements isInInsertionMode order ... SortingMachine1L SortingMachine5 size 8 February 2019 OSU CSE 9

  10. Interfaces and Classes There is really an abstract class Standard Iterable as usual in the chain here, but it is not shown because these extends extends slides describe the client view, and a client needs only SortingMachine- interface SortingMachine Kernel and a class like extends SortingMachine1L . SortingMachine implements implements ... SortingMachine1L SortingMachine5 8 February 2019 OSU CSE 10

  11. Mathematical Model SORTING_MACHINE_MODEL is ( insertion_mode: boolean , ordering: binary relation on T, contents: finite multiset of T ) exemplar m constraint IS_TOTAL_PREORDER(m.ordering) type SortingMachineKernel is modeled by SORTING_MACHINE_MODEL 8 February 2019 OSU CSE 11

  12. Mathematical Model SORTING_MACHINE_MODEL is ( insertion_mode: boolean , ordering: binary relation on T, contents: finite multiset of T ) exemplar m The mathematical model is an constraint ordered triple (a.k.a. three-tuple ): IS_TOTAL_PREORDER(m.ordering) a boolean , type SortingMachineKernel is modeled by a binary relation on T , and a finite multiset of T . SORTING_MACHINE_MODEL 8 February 2019 OSU CSE 12

  13. Mathematical Model SORTING_MACHINE_MODEL is ( insertion_mode: boolean , ordering: binary relation on T, contents: finite multiset of T ) exemplar m Recall: a binary relation on T may be constraint viewed as a set of ordered pairs of T , IS_TOTAL_PREORDER(m.ordering) or as a boolean -valued function R of type SortingMachineKernel is modeled by two parameters of type T that is true iff that pair is in the set. SORTING_MACHINE_MODEL 8 February 2019 OSU CSE 13

  14. Mathematical Model SORTING_MACHINE_MODEL is ( insertion_mode: boolean , ordering: binary relation on T, contents: finite multiset of T ) exemplar m A finite multiset is essentially a finite constraint set with multiple copies of elements IS_TOTAL_PREORDER(m.ordering) allowed, so there are effectively (non- type SortingMachineKernel is modeled by negative) “counts” of all values of the element type T ; details as necessary. SORTING_MACHINE_MODEL 8 February 2019 OSU CSE 14

  15. Mathematical Model SORTING_MACHINE_MODEL is ( insertion_mode: boolean , ordering: binary relation on T, contents: finite multiset of T ) exemplar m constraint IS_TOTAL_PREORDER(m.ordering) type SortingMachineKernel is modeled by SORTING_MACHINE_MODEL 8 February 2019 OSU CSE 15

  16. Review: Comparators • The Java interface Comparator<T> is: public interface Comparator<T> { /** * Returns a negative integer, zero, or * a positive integer as the first * argument is less than, equal to, or * greater than the second. */ int compare(T o1, T o2); } 8 February 2019 OSU CSE 16

  17. Review: Comparators • The notion of “less than” and “greater than” can be anything • The notion of “equal to” is actually supposed to be “equivalent to”, in the sense that the first argument is neither “less than” nor “greater than” the other • There are important technicalities ... 8 February 2019 OSU CSE 17

  18. Review: Creating a Comparator private static class IntegerLT implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { if (o1 < o2) { return -1; } else if (o1 > o2) { return 1; } else { return 0; } } } 8 February 2019 OSU CSE 18

  19. Review: Creating a Comparator private static class IntegerLT implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { if (o1 < o2) { A class that implements return -1; Comparator is usually a } else if (o1 > o2) { return 1; nested class (i.e., declared for } else { local use inside another class), return 0; and if so should be declared } private static . } } 8 February 2019 OSU CSE 19

  20. Review: Creating a Comparator private static class IntegerLT implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { return o1 – o2; } } Note that the results are not specified to be –1 for “less than” and +1 for “greater than”, but merely negative and positive values, respectively! Does this code work? 8 February 2019 OSU CSE 20

  21. Review: An Easy Comparator private static class IntegerLT implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } Since a generic parameter must be a reference type, } and each wrapper type T (here, Integer ) implements the interface Comparable<T> , each has a compareTo method that can be called like this; it simplifies the code for compare from the previous Comparator<T> implementation (using < ), if ≤ is the mathematical ordering we want. 8 February 2019 OSU CSE 21

  22. SortingMachine Constructor • The constructor has one parameter order of type Comparator<T> – Sorting is based on the ordering provided by the compare method from order • Requires: IS_TOTAL_PREORDER(order) • Ensures: this = ( true , order, {}) 8 February 2019 OSU CSE 22

  23. Example Code State Comparator<Integer> ci = new IntegerLT (); SortingMachine<Integer> si = new SortingMachine1L<>(ci); 8 February 2019 OSU CSE 23

  24. Example Code State Comparator<Integer> ci = new IntegerLT (); SortingMachine<Integer> si = new SortingMachine1L<>(ci); si = ( true , ci, {}) 8 February 2019 OSU CSE 24

  25. add void add(T x) • Adds x to the contents of this . • Aliases: reference x • Updates: this .contents • Requires: this .insertion_mode • Ensures: this .contents = # this .contents union {x} 8 February 2019 OSU CSE 25

  26. add void add(T x) • Adds x to the contents of this . For multisets (like this .contents ), the union operator means the • Aliases: reference x “counts” of all values are added. • Updates: this .contents • Requires: this .insertion_mode • Ensures: this .contents = # this .contents union {x} 8 February 2019 OSU CSE 26

  27. Example Code State si = ( true , ci, {13, 8}) x = 13 si.add(x); 8 February 2019 OSU CSE 27

  28. Example Code State si = ( true , ci, {13, 8}) x = 13 si.add(x); si = ( true , ci, {13, 13, 8}) x = 13 8 February 2019 OSU CSE 28

  29. Example Code State Note the alias created here, which you cannot see si = ( true , ci, {13, 8}) in the tracing table. x = 13 si.add(x); si = ( true , ci, {13, 13, 8}) x = 13 8 February 2019 OSU CSE 29

  30. changeToExtractionMode void changeToExtractionMode() • Change the mode of this from insertion to extraction. • Updates: this .insertion_mode • Requires: this .insertion_mode • Ensures: not this .insertion_mode 8 February 2019 OSU CSE 30

  31. Example Code State si = ( true , ci, {13, 13, 8}) si.changeToExtractionMode(); 8 February 2019 OSU CSE 31

  32. Example Code State si = ( true , ci, {13, 13, 8}) si.changeToExtractionMode(); si = ( false , ci, {13, 13, 8}) 8 February 2019 OSU CSE 32

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend