CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. ·...
Transcript of CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. ·...
![Page 1: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/1.jpg)
SortingMachine
8 February 2019 OSU CSE 1
![Page 2: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/2.jpg)
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
![Page 3: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/3.jpg)
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 3
FIFO and LIFO aretime-based orderings; a SortingMachine uses a
value-based ordering.
![Page 4: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/4.jpg)
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
![Page 5: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/5.jpg)
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 5
The Java libraries have similar methods to sort built-in arrays and other
“collections”.
![Page 6: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/6.jpg)
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
![Page 7: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/7.jpg)
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
![Page 8: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/8.jpg)
Interfaces and Classes
SortingMachine-Kernel
extends
Standard
extends
8 February 2019 OSU CSE 8
SortingMachine
implements
SortingMachine1L
Iterable
extends
SortingMachine5...
implements
![Page 9: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/9.jpg)
Interfaces and Classes
SortingMachine-Kernel
extends
Standard
extends
8 February 2019 OSU CSE 9
SortingMachine
Iterable
extends
implements
SortingMachine1L SortingMachine5...
implements
SortingMachineKernelhas contracts for six methods:
addchangeToExtractionMode
removeFirstisInInsertionMode
ordersize
![Page 10: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/10.jpg)
Interfaces and Classes
SortingMachine-Kernel
extends
Standard
extends
8 February 2019 OSU CSE 10
SortingMachine
Iterable
extends
implements
SortingMachine1L SortingMachine5...
implements
There is really an abstract class as usual in the chain here, but it
is not shown because these slides describe the client view,
and a client needs only interface SortingMachine
and a class like SortingMachine1L.
![Page 11: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/11.jpg)
Mathematical ModelSORTING_MACHINE_MODEL is (
insertion_mode: boolean,ordering: binary relation on T,contents: finite multiset of T)
exemplar m
constraintIS_TOTAL_PREORDER(m.ordering)
type SortingMachineKernel is modeled bySORTING_MACHINE_MODEL
8 February 2019 OSU CSE 11
![Page 12: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/12.jpg)
Mathematical ModelSORTING_MACHINE_MODEL is (
insertion_mode: boolean,ordering: binary relation on T,contents: finite multiset of T)
exemplar m
constraintIS_TOTAL_PREORDER(m.ordering)
type SortingMachineKernel is modeled bySORTING_MACHINE_MODEL
8 February 2019 OSU CSE 12
The mathematical model is an ordered triple (a.k.a. three-tuple):
a boolean,a binary relation on T, and
a finite multiset of T.
![Page 13: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/13.jpg)
Mathematical ModelSORTING_MACHINE_MODEL is (
insertion_mode: boolean,ordering: binary relation on T,contents: finite multiset of T)
exemplar m
constraintIS_TOTAL_PREORDER(m.ordering)
type SortingMachineKernel is modeled bySORTING_MACHINE_MODEL
8 February 2019 OSU CSE 13
Recall: a binary relation on T may be viewed as a set of ordered pairs of T, or as a boolean-valued function R of two parameters of type T that is true
iff that pair is in the set.
![Page 14: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/14.jpg)
Mathematical ModelSORTING_MACHINE_MODEL is (
insertion_mode: boolean,ordering: binary relation on T,contents: finite multiset of T)
exemplar m
constraintIS_TOTAL_PREORDER(m.ordering)
type SortingMachineKernel is modeled bySORTING_MACHINE_MODEL
8 February 2019 OSU CSE 14
A finite multiset is essentially a finite set with multiple copies of elements
allowed, so there are effectively (non-negative) “counts” of all values of the element type T; details as necessary.
![Page 15: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/15.jpg)
Mathematical ModelSORTING_MACHINE_MODEL is (
insertion_mode: boolean,ordering: binary relation on T,contents: finite multiset of T)
exemplar m
constraintIS_TOTAL_PREORDER(m.ordering)
type SortingMachineKernel is modeled bySORTING_MACHINE_MODEL
8 February 2019 OSU CSE 15
![Page 16: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/16.jpg)
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
![Page 17: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/17.jpg)
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
![Page 18: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/18.jpg)
Review: Creating a Comparatorprivate 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
![Page 19: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/19.jpg)
Review: Creating a Comparatorprivate 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 19
A class that implements Comparator is usually a
nested class (i.e., declared for local use inside another class),
and if so should be declared private static.
![Page 20: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/20.jpg)
Review: Creating a Comparatorprivate static class IntegerLT
implements Comparator<Integer> {@Override
public int compare(Integer o1, Integer o2) {
return o1 – o2;
}
}
8 February 2019 OSU CSE 20
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?
![Page 21: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/21.jpg)
Review: An Easy Comparatorprivate static class IntegerLT
implements Comparator<Integer> {@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
}
8 February 2019 OSU CSE 21
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.
![Page 22: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/22.jpg)
SortingMachine Constructor
• The constructor has one parameter orderof 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
![Page 23: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/23.jpg)
Example
8 February 2019 OSU CSE 23
Code State
Comparator<Integer> ci =new IntegerLT ();
SortingMachine<Integer> si =new SortingMachine1L<>(ci);
![Page 24: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/24.jpg)
Example
8 February 2019 OSU CSE 24
Code State
Comparator<Integer> ci =new IntegerLT ();
SortingMachine<Integer> si =new SortingMachine1L<>(ci);
si = (true, ci,{})
![Page 25: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/25.jpg)
addvoid 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
![Page 26: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/26.jpg)
addvoid 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 26
For multisets (like this.contents), the union operator means the
“counts” of all values are added.
![Page 27: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/27.jpg)
Example
8 February 2019 OSU CSE 27
Code Statesi = (true, ci,
{13, 8})x = 13
si.add(x);
![Page 28: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/28.jpg)
Example
8 February 2019 OSU CSE 28
Code Statesi = (true, ci,
{13, 8})x = 13
si.add(x);
si = (true, ci,{13, 13, 8})
x = 13
![Page 29: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/29.jpg)
Example
8 February 2019 OSU CSE 29
Code Statesi = (true, ci,
{13, 8})x = 13
si.add(x);
si = (true, ci,{13, 13, 8})
x = 13
Note the alias created here, which you cannot see
in the tracing table.
![Page 30: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/30.jpg)
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
![Page 31: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/31.jpg)
Example
8 February 2019 OSU CSE 31
Code Statesi = (true, ci,
{13, 13, 8})
si.changeToExtractionMode();
![Page 32: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/32.jpg)
Example
8 February 2019 OSU CSE 32
Code Statesi = (true, ci,
{13, 13, 8})
si.changeToExtractionMode();
si = (false, ci,{13, 13, 8})
![Page 33: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/33.jpg)
removeFirstT removeFirst()
• Removes and returns some “first” (“smallest”) element from the contents of this.
• Updates: this.contents• Requires:
not this.insertion_mode andthis.contents /= {}
• Ensures:removeFirst is in #this.contents andfor all x: T where (x is in this.contents)
(this.ordering(removeFirst, x)) andthis.contents = #this.contents \ {removeFirst}
8 February 2019 OSU CSE 33
![Page 34: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/34.jpg)
removeFirstT removeFirst()
• Removes and returns some “first” (“smallest”) element from the contents of this.
• Updates: this.contents• Requires:
not this.insertion_mode andthis.contents /= {}
• Ensures:removeFirst is in #this.contents andfor all x: T where (x is in this.contents)
(this.ordering(removeFirst, x)) andthis.contents = #this.contents \ {removeFirst}
8 February 2019 OSU CSE 34
For multisets (like this.contents), is in means the “count” of the
given value is positive, i.e., that value is an element of the multiset.
![Page 35: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/35.jpg)
removeFirstT removeFirst()
• Removes and returns some “first” (“smallest”) element from the contents of this.
• Updates: this.contents• Requires:
not this.insertion_mode andthis.contents /= {}
• Ensures:removeFirst is in #this.contents andfor all x: T where (x is in this.contents)
(this.ordering(removeFirst, x)) andthis.contents = #this.contents \ {removeFirst}
8 February 2019 OSU CSE 35
For multisets (like this.contents), \ means the “counts” of all values are subtracted (but all remain non-
negative).
![Page 36: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/36.jpg)
Example
8 February 2019 OSU CSE 36
Code Statesi = (false, ci,
{13, 13, 8})x = -425
x = si.removeFirst();
![Page 37: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/37.jpg)
Example
8 February 2019 OSU CSE 37
Code Statesi = (false, ci,
{13, 13, 8})x = -425
x = si.removeFirst();
si = (false, ci,{13, 13})
x = 8
![Page 38: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/38.jpg)
Example
8 February 2019 OSU CSE 38
Code Statesi = (false, ci,
{13, 13, 8})x = -425
x = si.removeFirst();
si = (false, ci,{13, 13})
x = 8
The element removed is a “first” element based on the IntegerLT ordering, i.e., a smallest integer value.
![Page 39: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/39.jpg)
Example: Remove Another
8 February 2019 OSU CSE 39
Code Statesi = (false, ci,
{13, 13})x = 8
x = si.removeFirst();
![Page 40: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/40.jpg)
Example: Remove Another
8 February 2019 OSU CSE 40
Code Statesi = (false, ci,
{13, 13})x = 8
x = si.removeFirst();
si = (false, ci,{13})
x = 13
![Page 41: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/41.jpg)
Example: Remove Another
8 February 2019 OSU CSE 41
Code Statesi = (false, ci,
{13, 13})x = 8
x = si.removeFirst();
si = (false, ci,{13})
x = 13
The element removed is a “first” element based on the
IntegerLT ordering, i.e., a smallest integer value; another
such element remains!
![Page 42: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/42.jpg)
isInInsertionMode
boolean isInInsertionMode()
• Reports whether this is in insertion mode.
• Ensures:isInInsertionMode =
this.insertion_mode
8 February 2019 OSU CSE 42
![Page 43: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/43.jpg)
order
Comparator<T> order()
• Reports Comparator<T> being used for sorting by this.
• Ensures:order = this.ordering
8 February 2019 OSU CSE 43
![Page 44: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/44.jpg)
size
int size()
• Reports the number of elements in this.contents.
• Ensures:size = |this.contents|
8 February 2019 OSU CSE 44
![Page 45: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/45.jpg)
size
int size()
• Reports the number of elements in this.contents.
• Ensures:size = |this.contents|
8 February 2019 OSU CSE 45
For multisets (like this.contents), |•| means the sum of the multiset’s
“counts” of all values.
![Page 46: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/46.jpg)
iterator
Iterator<T> iterator()
• Returns an iterator over a multiset of elements of type T.
• Ensures:multiset_entries(~this.seen * ~this.unseen) =
this.contents
8 February 2019 OSU CSE 46
![Page 47: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/47.jpg)
iterator
Iterator<T> iterator()
• Returns an iterator over a set of elements of type T.
• Ensures:multiset_entries(~this.seen * ~this.unseen) =
this.contents
8 February 2019 OSU CSE 47
multiset_entries is like entries for a string, except that each string entry’s “count” in the multiset is the same as
its (occurrence) count in the string.
![Page 48: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/48.jpg)
Temporal Flexibility
• The contract for SortingMachineKerneldoes not tell the client when “sorting” occurs, so an implementation of SortingMachineKernel may pay the cost of comparing elements to each other:– During add– During changeToExtractionMode– During removeFirst
8 February 2019 OSU CSE 48
![Page 49: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/49.jpg)
Instead of sort ...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 49
![Page 50: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/50.jpg)
Instead of sort ...while (/*input values remain*/) {
/* let x = next input value */
sorter.add(x);
}
sorter.changeToExtractionMode();
while (sorter.size() > 0) {
T x = sorter.removeFirst();
/* output x */}
8 February 2019 OSU CSE 50
![Page 51: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/51.jpg)
Instead of sort ...while (/*input values remain*/) {
/* let x = next input value */
sorter.add(x);
}
sorter.changeToExtractionMode();
for (int i = 0; i < 50; i++) {
T x = sorter.removeFirst();
/* output x */}
8 February 2019 OSU CSE 51
This is how you might remove only the “first 50” (“smallest 50”) values that were
added to sorter.
![Page 52: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/52.jpg)
Why Is This Better Than sort?
• If all elements are already sorted by the end of changeToExtractionMode, then there is no difference in performance compared to using sort
• If all elements are not already sorted by the end of changeToExtractionMode, then there can be a performance advantage if you don’t need to remove all the elements!– See a future project ...
8 February 2019 OSU CSE 52
![Page 53: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/53.jpg)
Sorting Algorithms
• Implementer of SortingMachineKernel may embed any sorting algorithm in it, e.g.:– insertion sort (add does all the work)– quicksort (changeToExtractionMode
does all the work)– selection sort (removeFirst does all the
work)
8 February 2019 OSU CSE 53
![Page 54: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/54.jpg)
Sorting Algorithms
• Implementer of SortingMachineKernel may embed any sorting algorithm in it, e.g.:– insertion sort (add does all the work)– quicksort (changeToExtractionMode
does all the work)– selection sort (removeFirst does all the
work)
8 February 2019 OSU CSE 54
Actually, any sorting algorithm could be used when sorting is done entirely in
changeToExtractionMode.
![Page 55: CSE 2231 - SortingMachineweb.cse.ohio-state.edu/.../slides/13.SortingMachine.pdf · 2019. 2. 8. · SORTING_MACHINE_MODEL. 8 February 2019 OSU CSE 13. Recall: a . binary relation](https://reader036.fdocuments.net/reader036/viewer/2022062605/5fd3a1db872dbc7dc231a92f/html5/thumbnails/55.jpg)
Resources
• OSU CSE Components API: SortingMachine– http://cse.osu.edu/software/common/doc/
8 February 2019 OSU CSE 55