The Binary Search by Mr. Dave Clausen

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
College of Information Technology & Design
College of Information Technology & Design
MATH 224 – Discrete Mathematics
The Merge Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen.
Lecture 22: Arrays (cont). 2 Lecture Contents: t Searching in array: –linear search –binary search t Multidimensional arrays t Demo programs t Exercises.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
The Quick Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen.
Searching and Sorting I 1 Searching and Sorting 1.
Searching. 2 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.
Algorithm Efficiency and Sorting
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 Arrays Linear search Binary search small arrays
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
The Insertion Sort Mr. Dave Clausen La Cañada High School.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Analysis of Algorithms
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
CSC 211 Data Structures Lecture 13
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
The Selection Sort Mr. Dave Clausen La Cañada High School.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Searching and Sorting Arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
19 Searching and Sorting.
Applied Discrete Mathematics Week 2: Functions and Sequences
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Towers of Hanoi Move n (4) disks from pole A to pole C
Search Recall the employee database used in class examples: employeeRecordT workforce[5] = Given "Vance, Emelline", how can we find her salary? Name.
The Sequential Search (Linear Search)
Teach A level Computing: Algorithms and Data Structures
Algorithm design and Analysis
Searching.
Mr. Dave Clausen La Cañada High School
Searching and Sorting Arrays
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Ken Lambert & Doug Nance
Searching CLRS, Sections 9.1 – 9.3.
Mr. Dave Clausen La Cañada High School
24 Searching and Sorting.
Searching and Sorting Arrays
Quicksort.
The Sequential Search (Linear Search)
The Sequential Search (Linear Search)
Searching.
Module 8 – Searching & Sorting Algorithms
Mr. Dave Clausen La Cañada High School
Presentation transcript:

The Binary Search by Mr. Dave Clausen 10001011110110000111 Mr. D. Clausen

GuessingGame.cpp GuessingGame.exe Let’s play a guessing game. You will enter the largest number that you wish to guess, and keep guessing until you find the random number between 1 and the largest number that you entered. This illustrates how the Binary Search finds an element in the list. GuessingGame.cpp GuessingGame.exe Mr. Dave Clausen

The Binary Search Description The binary search consists of examining a middle value of a list to see which half contains the desired value. The middle value of the appropriate half is then examined to see which half of the half contains the value in question. This halving process is continued until the value is located or it is determined that the value is not in the list. Mr. Dave Clausen Mr. D. Clausen

Binary Search Variations We will not use any variations of the Binary Search. We will only determine whether the target is in the list or not in the list. We will not find the index numbers of the target or how many occurrences there are in the list. Mr. Dave Clausen Mr. D. Clausen

Binary Search Assumptions The list must be sorted! This will allow us to find the middle data item in the list in constant time, by dividing the sum of the first index position and the last index position by two and using the subscript operation. Mr. Dave Clausen Mr. D. Clausen

Binary Search Uses Recursion The basic idea of binary search can be expressed recursively. If there are items in the list remaining to be examined, we compare the target value to the item at the middle position in the list. If the target value equals this item, we return the index position of the item. If the target value is greater than the item at the middle position, the target will be somewhere to the right of the middle position if it is in the list at all, so we recursively search the right half of the list. Otherwise, the target value will be to the left of the middle position if it is in the list at all, so we recursively search the left half of the list. If the target value is not in the list, we will run out of items to consider at the end of some recursive process, so we return the value -1. Mr. Dave Clausen Mr. D. Clausen

Binary Search Parameters There are four input parameters to the problem: the target item, the list, the index value of the first position in the list, and the index value of the last position in the list. There is one value to be returned: -1 indicating that we have not found the target item in the list or an integer indicating its index position if we have found it. The initial value of the first position is zero. The initial value of the last index position is the number of data items in the list minus one. Mr. Dave Clausen Mr. D. Clausen

A Binary Search Algorithm //Using Iteration If there are no more items to consider then Return -1 Else Set midpoint to (last + first) / 2 If the item at index midpoint = = the target item then Return midpoint Else if the item at index midpoint > the target item then Return search the left half of the list (from indices first to midpoint - 1) Return search the right half of the list (from indices midpoint + 1 to last) Mr. Dave Clausen Mr. D. Clausen

Binary Search Python Using Iteration Mr. Dave Clausen

Binary Search Python Using Recursion Mr. Dave Clausen

The Binary Search C ++ //Using Recursion int Binary_Search(int target, const apvector<int> &list, int first, int last) { if (first > last) return -1; else int midpoint = (first + last) / 2; if (list[midpoint] == target) return midpoint; else if (list[midpoint] > target) return Binary_Search(target, list, first, midpoint - 1); return Binary_Search(target, list, midpoint + 1, last); } Mr. Dave Clausen Mr. D. Clausen

Interface for Binary Search C++ For many of the recursive functions it is customary to start with an “interface” function, since recursive functions call themselves. Here is the “interface” for the Binary Search: int Bin_Search(int target, const apvector<int> &list) { return Binary_Search(target, list, 0, list.length() - 1); } You can use logical size - 1 instead of list.length() –1 if the logical and physical sizes are not the same. Don’t forget to pass logical size to the Bin_Search function. Mr. Dave Clausen Mr. D. Clausen

binsearch.cpp binsearch.txt Driver Program Here is a driver program to test the Binary Search source code: binsearch.cpp binsearch.txt Mr. Dave Clausen Mr. D. Clausen

Binary Search Walk Through Before continuing, let's walk through a binary search to better understand how it works. Assume list is the vector with values as indicated. Furthermore, assume target contains the value 205. Then initially, first, last, and target have the values Mr. Dave Clausen Mr. D. Clausen

Walk Through 2 A listing of values by each call of binsearch produces: Note that we need only four comparisons to find the target at the ninth position in the list. Mr. Dave Clausen Mr. D. Clausen

Walk Through 3 To illustrate what happens when the value being looked for is not in the vector, suppose target contains 210. The listing of values then produces: At this stage, first > last and the recursive process terminates. Mr. Dave Clausen Mr. D. Clausen

Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system. Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Binary Search is O(log2n ), because it takes approximately log2n passes to find the target element. Mr. Dave Clausen Mr. D. Clausen