A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University...
-
Upload
arron-daniel -
Category
Documents
-
view
215 -
download
1
Transcript of A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University...
![Page 1: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/1.jpg)
a review session2013-T2
Lecture 16School of Engineering and Computer Science, Victoria University of
Wellington
CO
MP 1
03
Marcus Frean
![Page 2: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/2.jpg)
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
![Page 3: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/3.jpg)
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
![Page 4: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/4.jpg)
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)
![Page 5: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/5.jpg)
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).
![Page 6: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/6.jpg)
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).
![Page 7: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/7.jpg)
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()]
![Page 8: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/8.jpg)
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?
![Page 9: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/9.jpg)
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;
![Page 10: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/10.jpg)
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:
![Page 11: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/11.jpg)
11
3 ways to loop – the third
List <String> mylist = new ArrayList <String>();
Instead, explicitly get and use an Iterator :
![Page 12: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/12.jpg)
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.
![Page 13: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/13.jpg)
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
![Page 14: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/14.jpg)
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>> ();
![Page 15: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/15.jpg)
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) {
}
![Page 16: A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.](https://reader036.fdocuments.us/reader036/viewer/2022082711/56649eae5503460f94bb5397/html5/thumbnails/16.jpg)
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) {
}