CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 10
Advertisements

Building Java Programs Chapter 13
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
1 Lecture 23 Searching Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Searching and Sorting I 1 Searching and Sorting 1.
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. 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 Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
CSE 143 Lecture 5 Binary search; complexity reading: slides created by Marty Stepp and Hélène Martin
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Building Java Programs Chapter 13 Searching reading: 13.3.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
CSC 205 Programming II Lecture 9 More on Recursion.
CSE 143 Lecture 6 Inheritance; binary search reading: 9.1, ; 13.1 slides created by Marty Stepp
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Binary search and complexity reading:
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 CSE 331 Comparing objects; Comparable, compareTo, and Comparator slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
Building Java Programs Chapter 13 Searching and Sorting Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Lecture 25: Searching and Sorting
Using recursion for Searching and Sorting
Sort & Search Algorithms
CSc 110, Autumn 2016 Lecture 36: searching.
Lecture 23:More on Interfaces
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
Collections.
Ch 14: Search and Sorting Yonglei Tao.
Adapted from slides by Marty Stepp and Stuart Reges
Searching.
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Building Java Programs Chapter 10
Building Java Programs
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
CSc 110, Spring 2017 Lecture 39: searching.
Building Java Programs
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
slides adapted from Marty Stepp
Topic 33 ArrayList.
Lecture 15: binary search reading:
CSE 143 Lecture 14 More Recursive Programming; Grammars
CSE 373 Data Structures and Algorithms
CSE 143 Lecture 16 (A) Searching and Comparable
slides created by Marty Stepp
Building Java Programs Chapter 13
Adapted from slides by Marty Stepp and Stuart Reges
Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration. How.
CSE 373 Objects in Collections: Object; equals; compareTo; mutability
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
CSE 143 Lecture 8 Iterators; Comparable reading: 11.2; 10.2
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Implementing ArrayIntList
Sorting Algorithms.
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp

2 Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration. –O(log N) runtime for an array of size N –can be implemented iteratively (with a loop) or recursively –Example: Searching the array below for the value 42: index value minmidmaxminmidmaxminmidmax

3 Binary search code // Returns the index of an occurrence of target in a, // or a negative number if the target is not found. // Precondition: elements of a are in sorted order public static int binarySearch(int[] a, int target) { int min = 0; int max = a.length - 1; while (min <= max) { int mid = (min + max) / 2; if (a[mid] < target) { min = mid + 1; } else if (a[mid] > target) { max = mid - 1; } else { return mid; // target found } return -(min + 1); // target not found }

4 Recursive binary search (13.3) Write a method binarySearch that accepts a sorted array of integers and a target integer value and returns the index of an occurrence of that target value in the array. –If the target value is not found, return a negative number. –Write the method recursively. int index = binarySearch(data, 42); // 10 int index2 = binarySearch(data, 66); // -1 or -14 index value

5 Exercise solution // Returns the index of an occurrence of the given value in // the given array, or a negative number if not found. // Precondition: elements of a are in sorted order public static int binarySearch(int[] a, int target) { return binarySearch(a, target, 0, a.length - 1); } // Recursive helper to implement search behavior. private static int binarySearch(int[] a, int target, int min, int max) { if (min > max) { return -1; // target not found } else { int mid = (min + max) / 2; if (a[mid] < target) { // too small; go right return binarySearch(a, target, mid + 1, max); } else if (a[mid] > target) { // too large; go left return binarySearch(a, target, min, mid - 1); } else { return mid; // target found; a[mid] == target }

6 Binary search and objects Suppose we want to modify the binarySearch method to search an array of Strings. –Operators like do not work with String objects. –But we do think of strings as having an alphabetical ordering. natural ordering: Rules governing the relative placement of all values of a given type. comparison function: Code that, when given two values A and B of a given type, decides their relative ordering: –A B

7 The compareTo method (10.2) The standard way for a Java class to define a comparison function for its objects is to define a compareTo method. –Example: in the String class, there is a method: public int compareTo(String other) A call of a.compareTo( b ) will return: a value <0if a comes "before" b in the ordering, a value >0if a comes "after" b in the ordering, or0if a and b are considered "equal" in the ordering.

8 Using compareTo compareTo can be used as a test in an if statement. String a = "alice"; String b = "bob"; if (a.compareTo(b) < 0) { // true... } PrimitivesObjects if (a < b) {...if (a.compareTo(b) < 0) {... if (a <= b) {...if (a.compareTo(b) <= 0) {... if (a == b) {...if (a.compareTo(b) == 0) {... if (a != b) {...if (a.compareTo(b) != 0) {... if (a >= b) {...if (a.compareTo(b) >= 0) {... if (a > b) {...if (a.compareTo(b) > 0) {...

9 Binary search w/ strings // Returns the index of an occurrence of target in a, // or a negative number if the target is not found. // Precondition: elements of a are in sorted order public static int binarySearch(String[] a, int target) { int min = 0; int max = a.length - 1; while (min <= max) { int mid = (min + max) / 2; if (a[mid].compareTo(target) < 0) { min = mid + 1; } else if (a[mid].compareTo(target) > 0) { max = mid - 1; } else { return mid; // target found } return -(min + 1); // target not found }

10 compareTo and collections You can use an array or list of Strings with Java's included binary search method because it calls compareTo internally. String[] a = {"al", "bob", "cari", "dan", "mike"}; int index = Arrays.binarySearch(a, "dan"); // 3 Set set = new TreeSet (); for (String s : a) { set.add(s); } System.out.println(s); // [al, bob, cari, dan, mike]

11 Ordering our own types We cannot use binarySearch or make a TreeSet / Map of any arbitrary type, because Java doesn't know how to order the elements. –The program compiles but crashes when we run it. Set tags = new TreeSet (); tags.add(new HtmlTag("body", true)); tags.add(new HtmlTag("b", false));... Exception in thread "main" java.lang.ClassCastException at java.util.TreeSet.add(TreeSet.java:238)

12 Comparable (10.2) public interface Comparable { public int compareTo( E other); } A class can implement the Comparable interface to define a natural ordering function for its objects. A call to your compareTo method should return: a value <0if the other object comes "before" this one, a value >0if the other object comes "after" this one, or0if the other object is considered "equal" to this. If you want multiple orderings, use a Comparator instead (see Ch. 13.1)

13 Comparable template public class name implements Comparable {... public int compareTo( name other) {... }

14 Comparable example public class Point implements Comparable { private int x; private int y;... // sort by x and break ties by y public int compareTo(Point other) { if (x < other.x) { return -1; } else if (x > other.x) { return 1; } else if (y < other.y) { return -1; // same x, smaller y } else if (y > other.y) { return 1; // same x, larger y } else { return 0; // same x and same y }

15 compareTo tricks subtraction trick - Subtracting related numeric values produces the right result for what you want compareTo to return: // sort by x and break ties by y public int compareTo(Point other) { if (x != other.x) { return x - other.x; // different x } else { return y - other.y; // same x; compare y } –The idea: if x > other.x,then x - other.x > 0 if x < other.x,then x - other.x < 0 if x == other.x,then x - other.x == 0 –NOTE: This trick doesn't work for double s (but see Math.signum )

16 compareTo tricks 2 delegation trick - If your object's fields are comparable (such as strings), use their compareTo results to help you: // sort by employee name, e.g. "Jim" < "Susan" public int compareTo(Employee other) { return name.compareTo(other.getName()); } toString trick - If your object's toString representation is related to the ordering, use that to help you: // sort by date, e.g. "09/19" > "04/01" public int compareTo(Date other) { return toString().compareTo(other.toString()); }

17 Exercises Make the Person class from Homework 3 comparable. –Compare people alphabetically by name, case-insensitively. Make the HtmlTag class from Homework 2 comparable. –Compare tags by their elements, alphabetically by name. –If two tags have the same element, opening tags should come before closing tags.