Post on 31-Dec-2015
a review session2013-T2
Lecture 16School of Engineering and Computer Science, Victoria University of
Wellington
CO
MP 1
03
Marcus Frean
2
But first: the cost of QuickSort
If Quicksort divides the array exactly in half, then: C(n) = n + 2 C(n/2) n log(n) comparisons
= O(n log(n)) (best case)
If Quicksort divides the array into 1 and n-1: C(n) = n + (n-1) + (n-2) + (n-3) + … + 2 + 1 = n(n-1)/2 comparisons
= O(n2) (worst case)
Average case? very hard to analyse. still O(n log(n)), and very good.
Quicksort is “in place”, MergeSort is not
3
Stable or Unstable? Faster if almost-sorted?
MergeSort: Stable: doesn’t jump any item over an unsorted region
⇒ two equal items preserve their order
Same cost on all input “natural merge” variant doesn’t sort already-sorted
regions⇒ will be very fast: O(n) on almost sorted lists
QuickSort: Unstable: Partition “jumps” items to the other end
⇒ two equal items likely to reverse their order
Cost depends on choice of pivot. sensible choice of pivot ⇒ O(n log(n)) on almost sorted
lists
4
Some Big-O costs revisitedImplementing Collections:
ArrayList: O(n) to add/remove, except at end
Stack: O(1)
ArraySet: O(n) (cost of searching)
SortedArraySet O( log(n) ) to search (with binary
search)
O(n) to add/remove (cost of moving
up/down)
O( n2 ) to add n itemsO( n log(n) ) to initialise with n items. (with fast sorting)
5
test: the warm-up question
Suppose you use a List called mypets to store information about your pets, represented as objects of type Pet. (Write code to declare and initialise an empty instance of such a list).
6
a bit tougher
Many people today have more than one phone number at which they might be reached. Suppose you wish to use a Map called phoneNums to store the set of phone numbers associated with each of your friends, who are represented by a class Person. The Map needs to be from keys that are objects of class Person, to sets of integers. (Write code to declare and initialise an empty instance of such a map).
7
Iterable vs Iterator
In words, describe the difference between these two interfaces.
Iterable ensures that the class IS iterable, meaning it has a method iterator() which will return an appropriate Iterator. This allows an object to be the target of the ``foreach'' statement.
Iterator ensures that the class IS an iterator, ie. it has methods next(), hasNext() [oh, and remove()]
8
Comparable vs Comparator
Java uses the method Collections.sort() to sort Lists into what is called a ``natural ordering''. What is the critical method that the class of objects in the list must have for this to work, and how is this ensured in Java?
9
3 ways to loop – the first
List <String> mylist = new ArrayList <String>();
Suppose this list has been populated by scanning a file. Write code that prints the strings, by going through the list using a standard ‘for’ loop: for (int i=0;
10
3 ways to loop – the second
List <String> mylist = new ArrayList <String>();
Write code that prints out the strings with a ``for each'' loop:
11
3 ways to loop – the third
List <String> mylist = new ArrayList <String>();
Instead, explicitly get and use an Iterator :
12
Carspublic class Car {
private int reg;
private Set <String> colours;
public Car(int registration, Set <String> cols) {
this.reg = registration;
this.colours = cols;
}
public Set<String> getColours() {
return colours;
}
}
Each car is identified by its unique registration number, and stores its colours in a Set.
13
a file about cars
Imagine you’re working on code to keep track of the Cars in a yard. Information on an individual car is stored on a single line, in a simple text file. Here is a short example:
Mini 75673 blue red yellow
Golf 93888 black blue
Uno 02009 red
Mini 44637 red black
The 1st question involved reading this and building a Map
14
a collection of collections
The class you’re working on involves a Map with a model (String) as its key, and a Set of Car objects as its value. Map <String, Set<Car> > modelsMap =
new HashMap <String, Set<Car>> ();
15
printAllColours()
Write code which uses the Map to generate and print out the set of all the colours that appear on at least one car. public void printAllColours(
Map <String,Set<Car>> modelsMap) {
}
16
printModelsGivenColour()
Write a new method, printModelsGivenColour(), which takes a colour (String) and the Map provided by mapModelToCars as arguments, and prints out the models in ascending order, without duplications.public void printModelsGivenColour(
String col, Map <String,Set<Car>> modelsMap) {
}