slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 13
Advertisements

CSE 143 Lecture 5 Binary search; complexity reading: slides created by Marty Stepp and Hélène Martin
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.
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
CSE 143 Lecture 6 Inheritance; binary search reading: 9.1, ; 13.1 slides created by Marty Stepp
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Binary search and complexity reading:
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.
Searching and Sorting Searching algorithms with simple arrays
Lecture 25: Searching and Sorting
Searching and Sorting.
CSc 110, Autumn 2016 Lecture 36: searching.
CSCI 162 – Introduction to Programming II William Killian
Dr. Kyung Eun Park Summer 2017
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 37: searching and sorting
Building Java Programs
Building Java Programs
Sorting Data are arranged according to their values.
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
this keyword this : A reference to the implicit parameter Syntax:
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
this keyword this : A reference to the implicit parameter Syntax:
CSc 110, Spring 2017 Lecture 39: searching.
Building Java Programs
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
slides adapted from Marty Stepp
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
CSc 110, Spring 2017 Lecture 39: searching and sorting
Sorting Data are arranged according to their values.
Building Java Programs
Lecture 15: binary search reading:
Building Java Programs
Building Java Programs
CSE 143 Lecture 14 More Recursive Programming; Grammars
Can we solve this problem?
CSE 373 Data Structures and Algorithms
CSE 143 Lecture 16 (A) Searching and Comparable
CSE 143 Lecture 4 More ArrayIntList:
Given value and sorted array, find index.
Lecture 15: Algorithms AP Computer Science Principles
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.
Can we solve this problem?
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
Lecture 21: Searching and Sorting
Building Java Programs
Building Java Programs
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
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Can we solve this problem?
CSE 143 Lecture 8 Iterators; Comparable reading: 11.2; 10.2
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList reading:
Suggested self-checks: Section 7.11 #12-29
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Implementing ArrayIntList
Sorting Algorithms.
Presentation transcript:

slides created by Marty Stepp CSE 143 Lecture 6 (b) Binary Search reading: 13.1 slides created by Marty Stepp http://www.cs.washington.edu/143/

Sequential search sequential search: Locates a target value in an array/list by examining each element from start to finish. How many elements will it need to examine? Example: Searching the array below for the value 42: Notice that the array is sorted. Could we take advantage of this? 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 i

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? 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

The Arrays class Class Arrays in java.util has many useful array methods: Syntax: Arrays.methodName(parameters) Method name Description binarySearch(array, value) returns the index of the given value in a sorted array (or < 0 if not found) binarySearch(array, minIndex, maxIndex, value) returns index of given value in a sorted array between indexes min /max - 1 (< 0 if not found) copyOf(array, length) returns a new resized copy of an array equals(array1, array2) returns true if the two arrays contain same elements in the same order fill(array, value) sets every element to the given value sort(array) arranges the elements into sorted order toString(array) returns a string representing the array, such as "[10, 30, -25, 17]"

Arrays.binarySearch // searches an entire sorted array for a given value // returns its index if found; a negative number if not found // Precondition: array is sorted Arrays.binarySearch(array, value) // searches given portion of a sorted array for a given value // examines minIndex (inclusive) through maxIndex (exclusive) Arrays.binarySearch(array, minIndex, maxIndex, value) The binarySearch method in the Arrays class searches an array very efficiently if the array is sorted. You can search the entire array, or just a range of indexes (useful for "unfilled" arrays such as the one in ArrayIntList) If the array is not sorted, you may need to sort it first

Using binarySearch // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int[] a = {-4, 2, 7, 9, 15, 19, 25, 28, 30, 36, 42, 50, 56, 68, 85, 92}; int index = Arrays.binarySearch(a, 0, 16, 42); // index1 is 10 int index2 = Arrays.binarySearch(a, 0, 16, 21); // index2 is -7 binarySearch returns the index where the value is found if the value is not found, binarySearch returns: -(insertionPoint + 1) where insertionPoint is the index where the element would have been, if it had been in the array in sorted order. To insert the value into the array, negate insertionPoint + 1 int indexToInsert21 = -(index2 + 1); // 6