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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Data Structures and Algorithms PLSD210 Sorting. Card players all know how to sort … First card is already sorted With all the rest, ¶Scan back from the.
CS 171: Introduction to Computer Science II Quicksort.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Sorting Chapter 10.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 373 Data Structures Lecture 15
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
(c) University of Washington14-1 CSC 143 Java Collections.
Recursion, Complexity, and Sorting By Andrew Zeng.
FASTER SORTING using RECURSION : QUICKSORT COMP 103.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
CSE 143 Lecture 11: Sets and Maps reading:
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
ISOM MIS 215 Module 1 – Ordered Lists. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Testing with JUnit, Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington 
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
CSE 143 Lecture 23: quick sort.
Describing algorithms in pseudo code
slides adapted from Marty Stepp
CS203 Lecture 15.
Algorithm Efficiency and Sorting
Presentation transcript:

a review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 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) + … = 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 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 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 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 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 3 ways to loop – the second List mylist = new ArrayList (); Write code that prints out the strings with a ``for each'' loop:

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

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