lecture5

# lecture5 - CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 5...

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

CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: 2010 Illinois Institute of Technology/George Koutsogiannakis 1

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

View Full Document
Last week’s topics Finding the Max and Min element value. Copying Arrays /Changing the size of an array. Searching Arrays. Selection Sort . 2
This week’ s topics Binary Search of an array. Using Arrays as counters. Vectors. Multi-dimensional Arrays 3

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

View Full Document
Binary Search A Binary Search is like the "Guess a Number" game. To guess a number between 1 and 100, we start with 50 (halfway between the beginning number and the end number). If we learn that the number is greater than 50, we immediately know the number is not 1 - 49. If we learn that the number is less than 50, we immediately know the number is not 51 - 100. We keep guessing the number that is in the middle of the remaining numbers (eliminating half the remaining numbers) until we find the number. 4
Binary Search The "Guess a Number" approach works because 1 - 100 are a "sorted" set of numbers. To use a Binary Search, the array must be sorted . Our Binary Search will attempt to find a search key in a sorted array. If the search key is found, we return the index of the element with that value. If the search key is not found,we return -1. 5

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

View Full Document
The Binary Search Algorithm We begin by comparing the middle element of the array and the search key. If they are equal, we found the search key and return the index of the middle element. If the middle element's value is greater than the search key, then the search key cannot be found in elements with higher array indexes. So, we continue our search in the left half of the array. If the middle element's value is less than the search key, then the search key cannot be found in elements with lower array indexes. So, we continue our search in the right half of the array. 6
The Binary Search Algorithm (cont’d) As we keep searching, the subarray we search keeps shrinking in size. In fact, the size of the subarray we search is cut in half at every iteration. If the search key is not in the array, the subarray we search will eventually become empty. At that point, we know that we will not find our search key in the array, and we return –1. 7

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

View Full Document
Example of a Binary Search We will search for the value 7 in this sorted array: To begin, we find the index of the center element, which is 8, and we compare our search key (7) with the value 45. 8
Because 7 is less than 45, we eliminate all array elements higher than our current middle element and consider elements 0 through 7 the new subarray to search. The index of the center element is now 3, so we compare 7 to the value 8. 9

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 06/27/2010 for the course CS 116 taught by Professor Koutsogiannakis during the Summer '08 term at Illinois Tech.

### Page1 / 43

lecture5 - CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 5...

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

View Full Document
Ask a homework question - tutors are online