Presentation is loading. Please wait.

Presentation is loading. Please wait.

A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.

Similar presentations


Presentation on theme: "A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean."— Presentation transcript:

1 a review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean

2 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(n 2 ) (worst case)  Average case ?  very hard to analyse.  still O(n log(n)), and very good.  Quicksort is “in place”, MergeSort is not

3 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 4 Some Big-O costs revisited Implementing Collections:  ArrayList: O(n) to add/remove, except at end  Stack:O(1)  ArraySet:O(n) (cost of searching)  SortedArraySetO( log(n) ) to search (with binary search) O(n) to add/remove (cost of moving up/down)  O( n 2 ) to add n items O( n log(n) ) to initialise with n items. (with fast sorting)

5 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 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 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 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 9 3 ways to loop – the first List mylist = new ArrayList (); 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 10 3 ways to loop – the second List mylist = new ArrayList (); Write code that prints out the strings with a ``for each'' loop:

11 11 3 ways to loop – the third List mylist = new ArrayList (); Instead, explicitly get and use an Iterator :

12 12 Cars public class Car { private int reg; private Set colours; public Car(int registration, Set cols) { this.reg = registration; this.colours = cols; } public Set getColours() { return colours; } Each car is identified by its unique registration number, and stores its colours in a Set.

13 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: Mini75673blueredyellow Golf93888blackblue Uno02009red Mini44637redblack The 1 st question involved reading this and building a Map 

14 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 > modelsMap = new HashMap > ();

15 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 > modelsMap) { }

16 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 > modelsMap) { }


Download ppt "A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean."

Similar presentations


Ads by Google