# 22-comparable - CSE 143 Lecture 22 Comparable and Searching...

This preview shows pages 1–6. Sign up to view the full content.

CSE 143 Lecture 22 Comparable and Searching reading: 13.1 - 13.3; 10.2 slides created by Marty Stepp http://www.cs.washington.edu/143/

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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. – 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 : 7 2 10 3 15 4 20 5 22 6 25 7 30 8 36 9 42 10 50 11 56 12 68 13 85 14 103 92 2 -4 value 16 15 1 0 index min mid max
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 }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Recursive binary search (13.3) • Write a recursive binarySearch method. – If the target value is not found, return its negative insertion point. int index = binarySearch(data, 42); // 10 int index2 = binarySearch(data, 66); // -14 7 2 10 3 15 4 20 5 22 6 25 7 30 8 36 9 42 10 50 11 56 12 68 13 85 14 103 92 2 -4 value 16 15 1 0 index
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 } } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/30/2011 for the course COMPUTER S 143 taught by Professor John during the Spring '11 term at Washington State Community College.

### Page1 / 18

22-comparable - CSE 143 Lecture 22 Comparable and Searching...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online