Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.

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

Introduction to Algorithms Quicksort
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Chapter 19: Searching and Sorting Algorithms
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 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.
Spring 2010CS 2251 Sorting Chapter 8. Spring 2010CS 2252 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn.
C++ Plus Data Structures
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
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.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
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.
Algorithm Efficiency and Sorting
Introduction to Computer Science Recursive Array Programming Recursive Sorting Algorithms Unit 16.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 8 ARRAYS Continued
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.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
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.
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.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
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.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Chapter 9 Searching and Sorting
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Data Structure Introduction.
Comparison-Based Sorting & Analysis Smt Genap
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
CS1101: Programming Methodology
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Searching and Sorting 14ACEHRPT Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Prof. U V THETE Dept. of Computer Science YMA
Sorting Mr. Jacobs.
Comparing Objects in Java
Chapter 13: Searching and Sorting
Describing algorithms in pseudo code
Outline Late Binding Polymorphism via Inheritance
C++ Plus Data Structures
Sorting Chapter 8 CS 225.
Sorting Chapter 8.
Review for Midterm 3.
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray Chapter 9: Sorting and Searching

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-2 Some Terminology sort key: The value used to sort the elements of a collection. Maybe be primitive or structured (primary and secondary keys) total ordering: The elements of the collection are said to be totally ordered if, for any elements x, y, and z from the collection, the following conditions are met: reflexive:x == x transitive: if x ≤ y and y ≤ z, then x ≤ z antisymmetric:if x ≤ y and y ≤ x, then x == y total:either x ≤ y or y ≤ x

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-3 Some Terminology increasing order (also called sorted or ascending order): The elements in the sorted collection are unique (no duplicates) and are ordered such that e 1 < e 2 < e 3 < …. < e n. For example: 2 < 5 < 16 nondecreasing order: The elements in the sorted collection may contain duplicates and are sorted such that e 1 ≤ e 2 ≤ e 3 ≤ …. ≤ e n. For example: 2 ≤ 5 ≤ 5 ≤ 21 decreasing order (also called reverse sorted or descending order): The elements in the sorted collection are unique (no duplicates) and are ordered such that e 1 > e 2 > e 3 > …. > e n. For example: 21 > 16 > 5 > 2

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-4 Some Terminology nonincreasing order: The elements in the sorted collection may contain duplicates and are sorted such that e 1 ≥ e 2 ≥ e 3 ≥ …. ≥ e n. For example: 21 ≥ 5 ≥ 5 ≥ 2 stable sort: Duplicates from the unsorted collection appear in the same relative order in the sorted collection. That is, if e i == e j and i < j, then e i will appear before e j in the sorted sequence. In the following example, the subscripts to 5 show their relative order in the unsorted sequence. Unsorted collection: Sorted, stable: Sorted, not stable: in-place sort: The extra space required to store the data during the sort is independent of the size of the input. The space complexity of such an algorithm is O(1)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-5 Selection Sort Pseudocode: selectionSort( primitiveType [] array ) 1. while the size of the unsorted part is greater than 1 2. find the position of the largest element in the unsorted part 3. move this largest element into the first position of the sorted part 4. decrement the size of the unsorted part by 1 // invariant: all elements from the first position to the last position of the // sorted part are in non-decreasing order First, let’s borrow an idea from the last chapter when we looked at searching. Split the “sort space” into sorted and unsorted parts.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-6 Selection Sort: The Picture (a) Initial configuration for selection sort. The input array is logically split into an unsorted part and a sorted part. (b) The array after the largest value from the unsorted part has been moved to the front of the sorted part (first pass). (c) The array after the largest value from the unsorted part has been moved to the front of the sorted part (second pass). (d) The array after the largest value from the unsorted part has been moved to the front of the sorted part (third pass).

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-7 Selection Sort Time Complexity # iterations of the inner loop per iteration of the outer loop 1 * * # iterations of the outer loop What should we count? How about comparison of elements? 1 *  (n) *  (n) =  (n 2 ) Space complexity: O(1) – only need space for local variables

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-8 Insertion Sort Pseudocode: insertionSort ( primitiveType [] array ) 1. while the size of the unsorted part is greater than 0 2. let the target element be the first element in the unsorted part 3. find target’s insertion point in the sorted part 4. insert the target in its final, sorted position // invariant: the elements from position 0 to (size of the sorted part – 1) // are in nondecreasing order Pseudocode finding the insertion point for the target in the sorted part 1. get a copy of the first element in the unsorted part 2. while ( there are elements in the unsorted part to examine AND we haven’t found the insertion point for the target ) 3. move the sorted element up a position // make room for the target

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-9 Insertion Sort: The Picture (a) Initial configuration for insertion sort. The input array is logically split into a sorted part (starting with one element) and an unsorted part. (b) The array after the first value from the unsorted part has been inserted in its proper position in the sorted part (first pass). (c) The array after the first value from the unsorted part has been inserted in its proper position in the sorted part (second pass). (d) The array after the first value from the unsorted part has been inserted in its proper position in the sorted part (third pass).

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-10 Insertion Sort Time Complexity Again, we count comparison of elements 1 *  (n) *  (n) =  (n 2 ) # iterations of the inner loop per iteration of the outer loop 1 * * # iterations of the outer loop Space complexity: O(1) – only need space for local variables

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-11 Comparison Based on Data Moves Instead of comparing element comparisons, what if we count the number of data moves made? Selection Sort always knows where the next element is going to go, but it doesn’t know which element to move. Worst case? n –1 swaps Insertion Sort knows which element to insert into the sorted part, but it doesn’t know where in the sorted part it will go. Worst case? O(n 2 ) swaps

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-12 Quick Sort The k th smallest value algorithm sorted the input to the point that the pivot for a partition ended up in position k – 1 If we continue with the partitioning step on the unsorted partitions, can we sort the input? Yes! This is the idea behind Quick Sort

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-13 Quick Sort where collection– the collection to be sorted first – the index of the first element of collection last – the index of the last element of collection pivotPosition = partition( collection, first, last ) QuickSort( collection, first, last ) QuickSort(collection, pivotPosition + 1, last ) QuickSort(collection, first, pivotPosition – 1 ) collection, if size(collection ) ≤ 1 if size( collection ) > 1 // base case // recursive case — sort left partition // recursive case — sort right partition

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-14 Quick Sort: Implementation 1 void quickSort( int[] array, int first, int last ) { 2 if ( first >= last ) // base case 3 return; 4 5 int pivotPosition = partition( array, first, last ); 6 7 // recursive case: sort left partition 8 quickSort( array, first, pivotPosition-1 ); 9 10 // recursive case: sort right partition 11 quickSort( array, pivotPosition+1, last ); 12 } As is common, the code follows the definition very closely

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-15 Quick Sort: An Example

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-16 Quick Sort: Using A “Good” Pivot If the pivot “evenly” splits the partition, there will be log 2 n levels, each of which requires O(n) comparisons: O(nlog 2 n) time complexity Space complexity: O(log 2 n) for the activation records

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-17 Quick Sort: Using A “Bad” Pivot If the pivot always produces one empty partition and one with n – 1 elements, there will be n levels, each of which requires O(n) comparisons: O(n 2 ) time complexity Space complexity: O(n) for the activation records

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-18 Merge Sort Since having partitions of the same size is so important to performance, is there some way we can guarantee equally sized performance? Yes! Idea: –Recursively split the partitions into equal chunks until each partition is of size 1 (so sorted) –As the recursion unwinds, merge the sorted partitions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-19 Merge Sort where collection – the collection to be sorted first – the index of the first element of coll midpoint – (first + last + 1) / 2 last – the index of the last element of coll MergeSort(collection, first, last ) MergeSort(collection, midpoint, last) MergeSort(collection, first, midpoint – 1) collection, if size( coll ) ≤ 1 if size( collection ) > 1 // base case // recursive case — sort left partition // recursive case — sort right partition merge ( collection, first, midpoint, last ) // merge left and right partitions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-20 Merge Sort: An Example Merge Sort follows a series of splitting steps with a series of merging steps to produce sorted partitions split steps merge steps

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-21 Merge Sort: Time/Space Complexity Time complexity: Merge Sort guarantees that each split produces partitions of the same size (or off by 1), guaranteeing there will be log 2 n levels There are n comparisons done on each level  O(nlog 2 n) comparisons are done Space complexity: a little trickier – extra space is needed to merge the elements, but this can be minimized. Still, O(n) space complexity

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-22 Comparison of Sorting Algorithms Algorithm Time ComplexitySpace Complexity Average CaseWorst CaseAverage CaseWorst Case Selection sort (n2)(n2) (n2)(n2)  (1) Insertion sort (n2)(n2) (n2)(n2)  (1) Quick sort  (nlog 2 n) (n2)(n2)  (log 2 n) (n)(n) Merge sort  (nlog 2 n) (n)(n) (n)(n)

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-23 Comparing Objects Sorting a collection of objects is different from sorting a collection of primitives The relational operators applied to objects compares the references, not the values of the objects Use the Comparable interface

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-24 The Comparable Interface public interface Comparable { int compareTo( T o ); } obj1.compareTo(obj2) returns  a negative integer if obj1 is “less than” obj2  a positive integer if obj1 is “greater than” obj2  zero if obj1 is “equal to” obj2 Test Using compareTo() to Compare Objects Equivalent Relational Operation to Compare Primitives a.compareTo( b ) < 0 a < b a.compareTo( b ) > 0 a > b a.compareTo( b ) == 0 a == b a.compareTo( b ) != 0 a != b a.compareTo( b ) <= 0 a <= b a.compareTo( b ) >= 0 a >= b

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-25 How to Make a Class Comparable // The String class declares that it implements // the Comparable interface so that an instance of // itself can be compared to another String. public class String implements Comparable Note the use of a specific type in the type parameter field. This guarantees that we can only compare Strings to Strings

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-26 Generic Methods Remember, just as classes can be generic, so can methods Declaration Application

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-27 Selection Sort as a Generic Method What should the type of the parameter be? First attempt: void selectionSort( T[], int n) Not quite right. The method doesn’t know enough about the actual type that will map to T We need to be sure that whatever type T ends up being, its elements are Comparable to one another

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-28 Selection Sort as a Generic Method What we need for our generic method is a way to say that instances of the actual type passed in for T are comparable to one another Second attempt: > void selectionSort( T[] a, int n ) This means selectionSort() can sort instances of any type T that implements Comparable

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-29 Selection Sort as a Generic Method Problem: The second attempt means that the class we provide for T must implement Comparable itself: It cannot have inherited the compareTo() method from a superclass that implemented Comparable. I want my sort method to be able to sort objects of type SuperType and SubType. The header in attempt 2 would only allow my to sort objects of type SuperType. 

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-30 Selection Sort as a Generic Method We need a way to say that either T implements Comparable itself or that some supertype of T implements Comparable, in which case T will inherit that class’s compareTo() method Solution: combine the ? wildcard with the keyword super to specify a type lower bound (e.g., describes a family of types bounded below by T ). Our header is now > void selectionSort(T[], int n) This means the method accepts an array of any type T (the lower bound) that implements Comparable or has a supertype (ancestor) that implements Comparable

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-31 Updating Selection Sort for Objects /** * Sort the array elements in ascending order * using selection sort. a - the array of integers to sort n – the number of elements in a to sort NullPointerException if a is null IndexOutOfBoundsException if n * is greater than a.length */ static void selectionSort( int[] a, int n ) { if ( a == null ) throw new NullPointerException(); if ( n > a.length ) throw new IndexOutOfBoundsException(); // while the size of the unsorted part is > 1 for ( int unsortedSize = n; unsortedSize > 1; unsortedSize-- ) { // find the position of the largest // element in the unsorted section int maxPos = 0; for (int pos = 1; pos < unsortedSize; pos++) if ( a[pos] > a[maxPos] ) maxPos = pos; // postcondition: maxPos is the position // of the largest element in the unsorted // part of the array // Swap largest value with the last value // in the unsorted part int temp = a[unsortedSize - 1]; a[unsortedSize - 1] = a[maxPos]; a[maxPos] = temp; } /** * Sort the array elements in ascending order * using selection sort. a - array of Comparable objects to sort n – the number of elements in a to sort NullPointerException if a is null IndexOutOfBoundsException if n * is greater than a.length */ static > void selectionSort( T[] a, int n ) { if ( a == null ) throw new NullPointerException(); if ( n > a.length ) throw new IndexOutOfBoundsException(); // while the size of the unsorted part is > 1 for ( int unsortedSize = n; unsortedSize > 1; unsortedSize-- ) { // find the position of the largest // element in the unsorted section int maxPos = 0; for (int pos = 1; pos < unsortedSize; pos++) if ( a[pos].compareTo(a[maxPos]) > 0 ) maxPos = pos; // postcondition: maxPos is the position // of the largest element in the unsorted // part of the array // Swap largest value with the last value // in the unsorted part T temp = a[unsortedSize - 1]; a[unsortedSize - 1] = a[maxPos]; a[maxPos] = temp; } Only the code in red had to be changed!

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-32 Making a Class Comparable Comparable defines a total ordering on the objects of a class that implements it This is called the class’s natural ordering and the compareTo() method of a class is called its natural comparison method It is considered good practice to make sure that x.equals(y) for all objects for which x.compareTo(y) == 0

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-33 Making Name Comparable The natural sort order for names is lexicographical by last name, then first name. If we override equals(), we must also override hashCode() to honor the contract for equals()

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-34 Making Name Comparable 1 /** 2 * The Name class stores a person’s first and last name. 3 */ 4 public class Name implements Comparable { 5 6 private String firstName; 7 private String lastName; 8 9 // CONSTRUCTORS, ACCESSORS, AND MUTATORS NOT SHOWN /** 12 * Determine if this name is equal to the given argument. 13 o The Name object to which we are to compare 14 * this Name 15 true if both the first and last name of this 16 * Name match the first and last names of the given 17 * argument; otherwise return false. 18 */ 19 public boolean equals( Object o ) { 20 if ( o instanceof Name ) 21 return this.compareTo( (Name)o ) == 0; 22 return false; 23 } make sure that x.equals(y ) for all objects for which x.compareTo(y) == 0 Ensure that we say that a Name is only Comparable to another Name

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-35 Making Name Comparable 25 /** 26 * Return the hashcode for this Name. 27 The hashcode for this name. 28 */ 29 public int hashCode(){ 30 return 31 * this.lastName.hashCode() + 31 this.firstName.hashCode(); 32 } By the contract for equals() in Object, if o1.equals(o2), then o1.hashCode == o2.hashCode. So, if we override equals(), we must override hashCode().

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-36 Making Name Comparable 34 /** 35 * Compare this Name to the Name supplied in 36 * the argument. 37 o The Name to which to compare this 38 * Name if this name is lexicographically less than the 40 * other name. 41 * 0 if they are equal 42 * 1 if this name is lexicographically greater than the 43 * other name 44 IllegalArgumentException if 45 * other is null 46 */ 47 public int compareTo( Name other ){ if ( other == null ) 50 throw new IllegalArgumentException(); int result = 0; 53 if ( this.lastName.compareTo( other.lastName() ) < 0 ) 54 result = -1; 55 else if (this.lastName.compareTo( other.lastName() ) == 0) 56 result = this.firstName.compareTo( other.firstName() ); return result; 59 } The natural sort order for names is lexicographical by last name, then first name

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-37 Searching for Objects The mechanisms used to sort objects, Comparable and Comparator, apply equally well to searching for objects in a collection We can modify the search algorithms from Chapter 8 to search for objects just as in this chapter we adapted sort algorithms for primitive types to sort objects: 1.Change the line that does the comparison of elements to use Comparable.compareTo() or Comparator.compare() 2.Change the method header

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-38 Example: binarySearch() for objects > int binarySearch( T[] array, int first, int last, T target ){ if ( first > last ) return -1; // Base case - failure if ( ( first = array.length ) || ( last = array.length ) ) throw new IndexOutOfBoundsException(); int mid = ( first + last ) / 2; if ( array[ mid ].compareTo( target ) == 0 ) return mid; // Base case - success // recursive cases if ( array[ mid ].compareTo( target ) < 0 ) return binarySearch( array, mid + 1, last, target ); else return binarySearch( array, first, mid - 1, target ); }