CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Sorting21 Recursive sorting algorithms Oh no, not again!
Searching and Sorting I 1 Searching and Sorting 1.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 8 ARRAYS Continued
Abstract Data Types (ADTs) Data Structures The Java Collections API
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Sorting and Searching.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Computer Science Searching & Sorting.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and 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.
Chapter 9 Searching and Sorting
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
CSCI 51 Introduction to Programming March 12, 2009.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CSCI 51 Introduction to Programming March 10, 2009.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Searching and Sorting Searching algorithms with simple arrays
Sorts, CompareTo Method and Strings
16 Searching and Sorting.
19 Searching and Sorting.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Sorting Mr. Jacobs.
Searching.
CSC 222: Object-Oriented Programming
Teach A level Computing: Algorithms and Data Structures
Quicksort 1.
CO 303 Algorithm Analysis And Design Quicksort
Searching.
CS 144 Advanced C++ Programming May 9 Class Meeting
Presentation transcript:

CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Sorting Animations  omparisonSort.html omparisonSort.html  2

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Quizzes for July 9  Quiz 13 July –

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #6 Solution  Three mutually recursive methods: getTermValue() getFactorValue() getExpressionValue()  Which one should handle the modulo % operator? 4

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #6 Solution, cont’d  The % modulo operator binds as tightly (has the same precedence level) as * and /  Therefore, method getTermValue() should also handle % 5

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #6 Solution, cont’d 6 public int getTermValue() { int value = getFactorValue(); boolean done = false; while (!done) { String next = tokenizer.peekToken(); if ("*".equals(next) || "/".equals(next) || "%".equals(next)) { tokenizer.nextToken(); int value2 = getFactorValue(); if ("*".equals(next)) { value = value*value2; } else if ("/".equals(next)) { value = value/value2; } else { value = value%value2; } } else { done = true; } } return value; }

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #6 Solution, cont’d  The ^ power operator binds the most tightly of all the operators. It has the highest precedence level.  How should we handle the modulo ^ operator? 7

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #6 Solution, cont’d  Because the ^ power operator has higher precedence than * / and %, it needs its own method. 8

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #6 Solution, cont’d 9 public int getPowerValue() { int value = getFactorValue(); boolean done = false; while (!done) { String next = tokenizer.peekToken(); if ("^".equals(next)) { tokenizer.nextToken(); int value2 = getFactorValue(); value = (int) Math.pow((double) value, (double) value2); } else { done = true; } } return value; }

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #6 Solution, cont’d  Who calls method getPowerValue() ?  Method getPowerValue() calls method getFactorValue().  Therefore, method getTermValue() calls method getPowerValue(). 10

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #6 Solution, cont’d 11 public int getTermValue() { int value = getPowerValue(); boolean done = false; while (!done) { String next = tokenizer.peekToken(); if ("*".equals(next) || "/".equals(next) || "%".equals(next)) { tokenizer.nextToken(); int value2 = getPowerValue(); if ("*".equals(next)) { value = value*value2; } else if ("/".equals(next)) { value = value/value2; } else { value = value%value2; } else { done = true; } return value; }

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7: Quicksort  Recall that quicksort works by selecting a pivot value from an array range, and it uses the pivot value to partition the range into two subranges. The first subrange will contain all the values less than the pivot value. The second subrange will contain all the values greater than the pivot value.  Then it recursively sort both partitions. 12

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7: Quicksort, cont’d  That’s what the textbook’s quicksort does: First partition: Then recursively sort each partition: 13

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7: Quicksort  A slightly more elegant version of partitioning places the pivot value between the two partitions.  When you do this, the pivot value is placed into its “final resting place”. It will not move again during subsequent sorting operations. 14

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak 15 Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7-Draft: Quicksort  For the draft, you are provided a partitioning method that places the pivot value into position.  Add more print statements to trace how: Index variables i and j march towards each other. Elements are swapped along the way. 16

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7-Draft: Quicksort 17 Partitioning: [50, 63, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 32, 54, 4, 88] Pivot = 50 [88, 63, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 32, 54, 4, 50] i = 0, j = 18, swapped 4 and 88: [4, 63, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 32, 54, 88, 50] i = 1, j = 16, swapped 32 and 63: [4, 32, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 63, 54, 88, 50] i = 4, j = 13, swapped 18 and 86: [4, 32, 29, 14, 18, 16, 79, 16, 26, 61, 47, 64, 83, 86, 97, 92, 63, 54, 88, 50] i = 6, j = 10, swapped 47 and 79: [4, 32, 29, 14, 18, 16, 47, 16, 26, 61, 79, 64, 83, 86, 97, 92, 63, 54, 88, 50] Partitioned: pivot = 50, pivot index = 9 [4, 32, 29, 14, 18, 16, 47, 16, 26, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61]

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7-Draft: Quicksort  You are provided a quicksort method.  Use your tracer partitioning method.  Add more print statements to trace each recursive call to sort(). For each recursive call, print the from and to index values for the start and end of the range to sort. 18

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7-Draft: Quicksort 19 Original array: [50, 63, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 32, 54, 4, 88] SORTING: from = 0, to = 19 Partitioning: [50, 63, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 32, 54, 4, 88] Pivot = 50 [88, 63, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 32, 54, 4, 50] i = 0, j = 18, swapped 4 and 88: [4, 63, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 32, 54, 88, 50] i = 1, j = 16, swapped 32 and 63: [4, 32, 29, 14, 86, 16, 79, 16, 26, 61, 47, 64, 83, 18, 97, 92, 63, 54, 88, 50] i = 4, j = 13, swapped 18 and 86: [4, 32, 29, 14, 18, 16, 79, 16, 26, 61, 47, 64, 83, 86, 97, 92, 63, 54, 88, 50] i = 6, j = 10, swapped 47 and 79: [4, 32, 29, 14, 18, 16, 47, 16, 26, 61, 79, 64, 83, 86, 97, 92, 63, 54, 88, 50] Partitioned: pivot = 50, pivot index = 9 [4, 32, 29, 14, 18, 16, 47, 16, 26, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61]

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7-Draft: Quicksort 20 SORTING: from = 0, to = 8 Partitioning: [4, 32, 29, 14, 18, 16, 47, 16, 26, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61] Pivot = 4 [26, 32, 29, 14, 18, 16, 47, 16, 4, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61] Partitioned: pivot = 4, pivot index = 0 [4, 32, 29, 14, 18, 16, 47, 16, 26, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61] SORTING: from = 0, to = -1 SORTING: from = 1, to = 8 Partitioning: [4, 32, 29, 14, 18, 16, 47, 16, 26, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61] Pivot = 32 [4, 26, 29, 14, 18, 16, 47, 16, 32, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61] i = 6, j = 7, swapped 16 and 47: [4, 26, 29, 14, 18, 16, 16, 47, 32, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61] Partitioned: pivot = 32, pivot index = 7 [4, 26, 29, 14, 18, 16, 16, 32, 47, 50, 79, 64, 83, 86, 97, 92, 63, 54, 88, 61] Note how pivot values don’t move after they’ve been placed into their final positions.

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7-Draft: Quicksort SORTING: from = 15, to = 16 Partitioning: [4, 14, 16, 16, 18, 26, 29, 32, 47, 50, 54, 61, 63, 64, 79, 83, 86, 88, 92, 97] Pivot = 83 [4, 14, 16, 16, 18, 26, 29, 32, 47, 50, 54, 61, 63, 64, 79, 86, 83, 88, 92, 97] Partitioned: pivot = 83, pivot index = 15 [4, 14, 16, 16, 18, 26, 29, 32, 47, 50, 54, 61, 63, 64, 79, 83, 86, 88, 92, 97] SORTING: from = 15, to = 14 SORTING: from = 16, to = 16 SORTING: from = 18, to = 17 SORTING: from = 19, to = 19 Sorted array: [4, 14, 16, 16, 18, 26, 29, 32, 47, 50, 54, 61, 63, 64, 79, 83, 86, 88, 92, 97]

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Homework #7-Final: Quicksort  For the final version, use quicksort to sort an array list of names. You are provided a text file of the names of U.S. presidents.  Sort the presidents by their last names. If two presidents have the same last name, sort those names by their first names. If two presidents have the same first and last names, sort their names by their middle names.  Example: George Bush should come before George W. Bush. 22

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Linear Search  Search for item in an array of n elements. The array is not sorted in any way.  What choices do we have? Look at all the elements one at a time. Example: To search in 1000 elements, it takes on average 500 steps.  Therefore: O(n) 23

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Binary Search  Now assume the array is sorted. Smallest value to largest value.  First check the middle element.  Is the target value you’re looking for smaller than the middle element? If so, search the first half of the array.  Is the target value you’re looking for larger than the middle element? If so, search the second half of the array. 24

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Binary Search, cont’d  The binary search keeps cutting in half the part of the array it’s searching. Next search either the first half or the second half. Eventually, you’ll either find the target value, or conclude that the value is not in the array.  Therefore, O(log 2 n) To search 1000 elements, it takes < 10 steps. Note: In computer science, logarithms are by default base 2. 25

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Recursive Binary Search  Binary search can be done recursively: 26 public static int search(int[] a, int low, int high, int value) { if (low <= high) { int mid = (low + high)/2; if (value == a[mid]) { return mid; } else if (value < a[mid]) { return search(a, low, mid-1, value); } else { return search(a, mid+1, high, value); } } else { return -1; } } Get the midpoint of the subrange. Found the target value? Search the first half. Search the second half. The target value is not in the subrange.

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Non-Recursive Binary Search  It’s easy to write binary search non-recursively: 27 public static int search(int[] a, int low, int high, int value) { while (low <= high) { int mid = (low + high)/2; if (value == a[mid]) { return mid; } else if (value < a[mid]) { high = mid-1; } else { low = mid+1; } } return -1; } Get the midpoint of the subrange. Found the target value? Search the first half next. Search the second half next. The target value is not in the array.

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Break 28

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Sorting and Searching in the Real World  So far, we’ve done sorting and searching with arrays of numeric values.  Revelation #1: In a real application, we may need to sort and search different object types. Example: Sort employee objects.  Revelation #2: Java has built-in sorting and searching routines. You don’t have to write them yourself! 29

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Built-in Sort Routines  Arrays.sort() will sort an array of objects. Example:  Collections.sort() will sort an array list of objects. Example: 30 Employee workers[] =... ; Arrays.sort(workers); ArrayList workers =... ; Collections.sort(workers);

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Built-in Sort Routines, cont’d  But how do Arrays.sort() and Collections.sort() know how to compare your objects? How does it know that object A is less than, equal to, or greater than object B?  Your objects must be Comparable objects. They must be instances of a class that implements the Comparable interface.  Arrays.sort() and Collections.sort() call the objects’ compareTo() methods 31

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Arrays.sort()  We saw Arrays.sort() on the midterm: 32 import java.util.Arrays; public class Name implements Comparable { private String name; private String first; private String last;... public int compareTo(Object other) { Name otherName = (Name) other; return this.last.compareTo(otherName.last); } public static void main(String args[]) { Name names[] = {... }; Arrays.sort(names); for (Name name : names) System.out.println(name.getName()); } } Use the String class’s compareTo() method.

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Collections.sort()  Collections.sort() sorts an array list: 33 import java.util.ArrayList; import java.util.Collections; public class Name implements Comparable {... public int compareTo(Object other) { Name otherName = (Name) other; return this.last.compareTo(otherName.last); } public static void main(String args[]) { ArrayList names = new ArrayList (); names.add(new Name("George Washington")); names.add(new Name("Abraham Lincoln"));... Collections.sort(names); for (Name name : names) System.out.println(name.getName()); } }

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Comparable as a Parameterized Interface  Just like ArrayList, we can give Comparable a type parameter.  So instead of: 34 public int compareTo(Object other) { Name otherName = (Name) other; return this.name.length() - otherName.name.length(); } Type cast required!

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Comparable as a Parameterized Interface  We can eliminate the type cast in the compareTo() method: 35 public class Name implements Comparable {... public int compareTo(Name otherName) { return this.last.compareTo(otherName.last); }... } No type cast!

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Sorting Challenges  Suppose you want to sort objects that don’t belong to a class that implements the Comparable interface. You don’t have access to the code to modify it.  Or, you are developing an application where an array or array list of objects needs to be sorted in different ways. 36

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak The Comparator Interface  Use the Comparator interface to create a comparator object whose sole responsibility is to compare two objects of the same type. The objects don’t have to belong to a class that implements the Comparable interface.  You can create different Comparator objects each of which compares in a different way. Tip: You can use inner classes that implement the Comparator interface. 37

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak The Comparator Interface, cont’d 38 import java.util.Arrays; import java.util.Comparator; public class Name { private String name; private String first; private String last;... private static class LastNamesComparator implements Comparator { public int compare(Name name1, Name name2) { return name1.last.compareTo(name2.last); } }

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak The Comparator Interface, cont’d 39 private static class ReverseLastNamesComparator implements Comparator { public int compare(Name name1, Name name2) { return -name1.last.compareTo(name2.last); } } private static class NameLengthsComparator implements Comparator { public int compare(Name name1, Name name2) { return name1.name.length() - name2.name.length(); } }

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak The Comparator Interface, cont’d 40 public static void main(String args[]) { Name names[] = { new Name("George Washington"), new Name("Abraham Lincoln"),... }; System.out.println("Sorted by last names:\n"); Arrays.sort(names, new LastNamesComparator()); for (Name name : names) System.out.println(name.getName()); System.out.println("\nReverse sorted by last names:\n"); Arrays.sort(names, new ReverseLastNamesComparator()); for (Name name : names) System.out.println(name.getName()); System.out.println("\nSorted by name lengths:\n"); Arrays.sort(names, new NameLengthsComparator()); for (Name name : names) System.out.println(name.getName()); } } Demo

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Measuring Algorithm Performance  How can we measure the performance of an algorithm? How much time does it take to run? How many operations does it perform?  We want to be able to estimate an algorithm’s performance without actually running it. It may be too expensive to run. 41

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Growth Order  We want to know an algorithm’s growth order. How does its performance change relative to the number N of objects it has to work with? If we increase N, how does its run time or number of operations grow?  We use big-Oh notation to indicate growth order. O(N) O(N 2 ) O(N log N) 42

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Growth Order, cont’d  Before choosing an algorithm to handle N items, make sure you understand the algorithm’s growth order as N increases.  The running time of an O(N), O(log N), or O(N log N) algorithm grows much more slowly than an O(N 2 ) algorithm.  See nformation/Handouts/order.html nformation/Handouts/order.html 43

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Growth Order, cont’d 44  Ten orders of growth aa/CS F/Information/Ha ndouts/order.html

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Growth Order, cont’d  The explosive growth of 2 n  The Explosive Growth of n! 45 aa/CS F/Information/Ha ndouts/order.html

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Growth Order, cont’d  Linear time: O(N) The algorithm makes one pass over the N elements and performs a “simple” operation per element. Example: Searching a list.  Quadratic time: O(N 2 ) The algorithm makes one pass over the N elements and performs up to N operations per element. Example: selection sort  Logarithmic time: O(log N) or O(N log N) The algorithm uses a “divide and conquer” strategy. Examples: merge sort, quicksort, binary search 46