Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Theory
Advertisements

Searching Algorithms Finding what you are looking for.
HST 952 Computing for Biomedical Scientists Lecture 9.
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
B+ Tree What is a B+ Tree Searching Insertion Deletion.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
Copyright Prentice Hall Modified by Sana odeh, NYU
16 Searching and Sorting.
Searching and Sorting Algorithms
Growth of Functions & Algorithms
Searching Given a collection and an element (key) to find… Output
Recitation 13 Searching and Sorting.
Search Lesson Outline Searching Lesson Outline
CS 3343: Analysis of Algorithms
COMP 53 – Week Seven Big O Sorting.
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Here is a puzzle I found on a t-shirt
Teach A level Computing: Algorithms and Data Structures
Sorting Data are arranged according to their values.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Sorting Algorithms Written by J.J. Shepherd.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
CS 3343: Analysis of Algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
i206: Lecture 8: Sorting and Analysis of Algorithms
Last Class We Covered Data representation Binary numbers ASCII values
CSc 110, Spring 2017 Lecture 39: searching.
Algorithms September 28, 2017.
Binary Search Example with Labeled Indices
Chapter 8 Search and Sort
Binary Search and Intro to Sorting
كلية المجتمع الخرج البرمجة - المستوى الثاني
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
Sorting Data are arranged according to their values.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Search,Sort,Recursion.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Given value and sorted array, find index.
Chapter 4.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Unit 2: Computational Thinking, Algorithms & Programming
Tutorial 6 Array Problem Solving
Principles of Computing – UFCFA3-30-1
Data Structures and Algorithms CS 244
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you go about that? If you wanted to look up someone with the last name “Smith,” you could flip through the phonebook one page at a time. You don’t need to be a computer scientist to know that this is an inefficient approach. Instead, we could start by flipping to the middle of the phonebook, and let's say we turn to a page with "M" on it. Since we know "Smith" comes after "Malan," we can literally tear the phonebook in half, throw away the left half of the phonebook, and leave ourselves with only the right half. We've just broken the problem in two! Once again, we flip to the middle and find ourselves at “R.” We can again throw away the left half. As we continue tearing the book in half and throwing away pieces of it, we will eventually be left with a single page on which the name “Smith” appears (assuming it was there in the first place).

How do these two approaches compare in terms of their times to solve the problem? In the graph below, the first steep line (n in red) represents the approach of turning one page at a time. The second steep line (n/2 in yellow) represents a slightly improved approach of turning two pages at a time. The curve (log n in green) represents our “tear and throw away” approach. As the size of the problem grows, the time to solve that problem doesn’t grow nearly as quickly. In the context of this problem, n is the number of pages in the phonebook. As we go from 500 to 1000 to 2000 pages in the phonebook, we need only tear the phonebook in half at most one or two more times.

1 3 5 6 7 9 10 Lin Does the array contain 7? 1 2 3 4 5 6 1 2 3 4 5 6 1 3 5 6 7 9 10 We can apply the same logic to searching for a value in an array of sorted numbers.

1 3 5 6 7 9 10 Lin Is array[3] == 7? Is array[3] < 7? 1 2 3 4 5 6 1 3 5 6 7 9 10 First, check the value stored at the middle index of the array. We see that array[3] is 6, which is < 7. So, just as we tore off half the phone book, we can now disregard the entire left half of this array. Is array[3] == 7? Is array[3] < 7? Is array[3] > 7? Lin

1 3 5 6 7 9 10 Lin Is array[5] == 7? Is array[5] < 7? 1 2 3 4 5 6 1 3 5 6 7 9 10 Next, check the value stored at the middle index of what's left of the array. We can see that array[5] is 9, which is > 7. This time, we'll discard the right portion of what's left of the array. Is array[5] == 7? Is array[5] < 7? Is array[5] > 7? Lin

1 3 5 6 7 9 10 Lin Is array[4] == 7? Is array[4] < 7? 1 2 3 4 5 6 1 3 5 6 7 9 10 array[4] == 7! We've found the value we were searching for! This algorithm takes log n steps in the worst case. When there are only 7 array indices to check, n might not seem all that much bigger than log n. But if we have a 4 billion indices, the difference certainly matters. In that case, linear search would take 4 billion steps in the worst case, while binary search would only take 32. Note however, that these arrays were presorted. We'll next cover sorting algorithms like bubble sort, merge sort, insertion sort, selection sort, and quick sort that can be employed if you're not lucky enough to start out with a sorted dataset! Is array[4] == 7? Is array[4] < 7? Is array[4] > 7? Lin