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.

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 13
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Computer Science 209 Software Development Equality and Comparisons.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
Building Java Programs Chapter 13 Searching reading: 13.3.
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
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:
Building Java Programs Chapter 13 Searching and Sorting Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
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.
Building Java Programs Interfaces reading: , 16.4.
Lecture 25: Searching and Sorting
Searching and Sorting.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 23:More on Interfaces
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
Interfaces.
Mid Term Review Advanced Programming Ananda Gunawardena
Adapted from slides by Marty Stepp and Stuart Reges
Wednesday Notecards What’s your credit card number?
Lecture 1: Welcome to CSE 373
slides created by Marty Stepp and Hélène Martin
Interfaces EE 422C.
Collections class Method name Description binarySearch(list, value)
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Building Java Programs
Lecture 14: binary search and complexity reading:
TCSS 143, Autumn 2004 Lecture Notes
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
Interfaces CS163 Fall 2018.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
slides adapted from Marty Stepp
Lecture 15: binary search reading:
CSE 154 Sorting reading: 13.3, 13.4
Collections in Java The objectives of this lecture are:
Lecture 5: complexity reading:
CSE 373 Data Structures and Algorithms
slides created by Marty Stepp
Collections class Method name Description binarySearch(list, value)
CSE 143 Lecture 16 (A) Searching and Comparable
Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations are common to all shapes:
Building Java Programs
slides created by Marty Stepp and Ethan Apter
slides created by Marty Stepp
Given value and sorted array, find index.
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
Based on slides by Alyssa Harding & Marty Stepp
Building Java Programs Chapter 13
Adapted from slides by Marty Stepp and Stuart Reges
slides created by Marty Stepp and Hélène Martin
CSE 373 Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
slides created by Marty Stepp
CSE 143 Lecture 8 Iterators; Comparable reading: 11.2; 10.2
Collections class Method name Description binarySearch(list, value)
CSE 143 Lecture 6 interfaces; eclipse; testing
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Sorting Algorithms.
Presentation transcript:

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 many elements will it need to examine? O(log N) Can be implemented with a loop or recursively Example: Searching the array below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 min mid max

Collections class Method name Description binarySearch(list, value) returns the index of the given value in a sorted list (< 0 if not found) copy(listTo, listFrom) copies listFrom's elements to listTo emptyList(), emptyMap(), emptySet() returns a read-only collection of the given type that has no elements fill(list, value) sets every element in the list to have the given value max(collection), min(collection) returns largest/smallest element replaceAll(list, old, new) replaces an element value with another reverse(list) reverses the order of a list's elements shuffle(list) arranges elements into a random order sort(list) arranges elements into ascending order

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 < 0 if A comes "before" B in the ordering, a value > 0 if A comes "after" B in the ordering, 0 if A and B are considered "equal" in the ordering.

Interfaces (9.5) interface: A list of methods that a class can promise to implement. Inheritance gives you an is-a relationship and code sharing. A Lawyer can be treated as an Employee and inherits its code. Interfaces give you an is-a relationship without code sharing. A Rectangle object can be treated as a Shape but inherits no code. Analogous to non-programming idea of roles or certifications: "I'm certified as a CPA accountant. This assures you I know how to do taxes, audits, and consulting." "I'm 'certified' as a Shape, because I implement the Shape interface. This assures you I know how to compute my area and perimeter."

Comparable (10.2) public interface Comparable<E> { 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 < 0 if the this object comes "before" other one, a value > 0 if the this object comes "after" other one, 0 if the this object is considered "equal" to other.