Warning This class is being recorded. At least I think it is. It’s probably also being transcribed.
Approximate overview
Academic
Cultural
Peer
Wellness
Misc
Sam did not record the questions about registration.
What does it mean to sort?
Many things:
In CS, we usually mean “put things in order”.
What kinds of things do we sort?
What should a sorting method signature look like in Java?
public ___ ___ ... sort(T[] values, Comparator<? super T>, ...)
What in heck does <? super T> mean?
AssociativeArray<String,Person> classlist;Comparator<T>, we’d know that it means “things
that compare T values”.Fraction class. We might write a comparator
for Fractionspublic class FractionComparator implements Comparator<Fraction> {
public int compare(Fraction f1, Fraction f2) {
BigInteger tmp1 = f1.numerator().multiply(f2.denominator());
BigInteger tmp2 = f2.numerator().multiply(f1.denominator());
return tmp1.compareTo(tmp2);
} // compare
} // FractionComparator
Now, suppose we’ve decided to extend our Fraction class.
public class BetterFraction extends Fraction {
// ...
} // BetterFraction
Can I apply FractionComparator objects to two BetterFraction values?
BetterFraction f1 = ...;
BetterFraction f2 = ...;
FractionComparator fc = new FractionComparator();
if (fc.compare(f1,f2) == 0) {
System.err.println("f1 and f2 are the same");
}
Yes! Subtype polymorphism says that if a procedure can accecpt type X as input, it can also accept any subclass of X (or subclass or subclass of X or …).
Back to
public ___ ___ ... sort(T[] values, Comparator<? super T>, ...)
We can sort an array of BetterFraction values using FractionComparator.
However, FractionComparator is not a Comparator<BetterFraction>,
it’s a Comparator<Fraction>.
<? super T> is Java’s way of handling that situation. It says that we
can use a Compator<T> or a comparator of a superclass of T, or a comparator
of a superclass of a superclass of T, or …
It will also work for any interfaces T implements.
Comparators are functional interface …
Here’s what we have so far.
public ___ ___ ... sort(T[] values, Comparator<? super T>, ...)
public ___ ___ void sort(T[] values, Comparator<? super T>)
If we put sort in a class (e.g., Sortable, we probably don’t need
any other modifiers.
public interface Sorter<T> {
public void sort(T[] values, Comparator<? super T>);
} // interface Sorter<T>
However, if we make sort a static method, …
public static ___ void sort(T[] values, Comparator<? super T>)
we do need something between static and void.
public static <T> void sort(T[] values, Comparator<? super T>)
We will focus on using the interface (or a variant thereof).
How do you now that a particular sort has succeeded? (in English)
(Not the algorithm, but the results of having sorted.)
What do you know about the result?
“It’s a sorted version of the original array.”
That is, the values appear in the order given by the comparator.
Do we need to ensure anything else (other than that the values appear in the order given by the compareator)?
Hypothesis S: If we sort again, nothing should change.
Question regarding hypothesis S: If I sort strings by length, and only
by length and I do not require that the sort is stable. What should
I get for the array { "hello", "alpha", "gamma" }?
Answer: Any order.
Critique of Hypothesis S: A valid sorting algorithm may still rearrange equal elements.
Do we need to ensure anything else (other than that the values appear in the order given by the comparator)?
We shouldn’t make a new array (may be implied by the signature) .
public static <T> void badSort(T[] values, Comparator<? super T> compare) {
for (int i = 1; i < values.length; i++) {
values[i] = values[0];
} // for
} badSort
We normally require that we neither add new elements nor remove existing elements. That is, the resulting array is a permutation of the original array.
How do you now that a particular sort has succeeded? (in Java)
Fraction[] stuff = { ... };
FractionCompator fc = new FractionComparator()
sort(stuff, fc);
We can validate the “in-order” property by going through the whole array, using the comparator to check neighboring values.
for (int i = 0; i < stuff.length-1; i++) {
if (fc.compare(stuff[i], stuff[i+1]) > 0) {
System.err.println("The sort failed");
break; // Make Prof. Johnson cry along with those whose classes got changed.
}
}
We can validate the “is a permutation” property with …
What approaches do we have to design sorting algorithms?
Warning! At the start of the next class, I’ll ask you about other approaches you use when you have to design an algorithm.
What restrictions might we put on a sorting algorithm?
stable, requiring that equal values maintain their
order.What kinds of tests can you run to ensure that your sorting algorithm works correctly?
You have an array with three values, which we’ll call red, white, and blue. They are in no particular order. Your goal is to rearrange it so that all of the red are at the left, all the blue are at the right, and all the white are in between.
You may not simply count the values; different reds represent different objects (similarly for blues and whites).
Sample input:
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| Ba | Rb | Wc | Wd | Re | Bf | Wg | Rh | Wi | Wj | Wk | Rl | Wm | Bn | Bo | Wp | Rq |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
How do you approach the problem?