Binary Search Binary Search Algorithm

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Arrays Linear search Binary search small arrays
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Building Java Programs Chapter 13 Searching reading: 13.3.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
RECURSION Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Dictionaries CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved
OCR A Level F453: Data structures and data manipulation Data structures and data manipulation a. explain how static data structures may be.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Merge Sort.
Binary Search.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Chapter 5 Ordered List.
COMP 53 – Week Seven Big O Sorting.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Searching an Array: Binary Search
Chapter 7 Single-Dimensional Arrays
Searching & Sorting "There's nothing hidden 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.
Search Algorithms Sequential Search (Linear Search) Binary Search
Chapter 5 Ordered List.
Lecture 14: binary search and complexity reading:
CSC215 Lecture Algorithms.
Lecture 11 Searching and Sorting Richard Gesick.
Lecture 15: binary search reading:
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
HKOI 2005 Intermediate Training
Searching CLRS, Sections 9.1 – 9.3.
Linear Search Binary Search Tree
Binary Search A binary search algorithm finds the position of a specified value within a sorted array. Binary search is a technique for searching an ordered.
Data Structures Sorted Arrays
Data Structures: Searching
Algorithms Lakshmish Ramaswamy.
Design and Analysis of Algorithms. Presentation Topic Divide & Conquer Paradigm to design/develop algorithms.
Module 8 – Searching & Sorting Algorithms
Searching.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Binary Search Binary Search Algorithm Performance, Advantages, Disadvantages Examples in Java and C++

Finding a word in a Dictionary 1) Linear Search - Search every word in the dictionary from the beginning to the end looking for the word that matches the word you are looking for (aka the key word). How long will it take (aka time complexity)? If the key is in the dictionary, on average, the amount of time needed to search half the words in the dictionary. T(n) = c * 𝑛 2 If the key is not in the dictionary, the amount of time needed to search every word. T(n) = c * n c = the time it takes to consider 1 word, n = numbers of words in the dictionary

Finding a word in a Dictionary 2) Binary Search – if the dictionary lists the words in alphabetical order Step 1) open dictionary to the middle word, compare it to key word if the key and the middle word match, you are done if the key < middle word, repeat step 1 on the left side words if the key > middle word, repeat step 1 on the right side words How much time does this take: T(n) = c + T( 𝑛 2 ) c = the time it takes to consider 1 word, n = numbers of words in the dictionary

Searching an unsorted array for 57

Binary search for 23

Binary Search in Java class BinarySearch { int binarySearch(int arr[], int left, int right, int key) { if (right >= left) { int mid = left + ((right - left) / 2); if (arr[mid] == key) return mid; if (arr[mid] > key) { return binarySearch(arr, left, mid - 1, key); } else { return binarySearch(arr, mid + 1, right, key); } } return -1;

Binary Search in C++ #include <iostream> using namespace std; int binarySearch(int arr[], int left, int right, int key) { if (right >= left) { int mid = left + ((right - left) / 2); if (arr[mid] == key) return mid; if (arr[mid] > key) { return binarySearch(arr, left, mid - 1, key); } else { return binarySearch(arr, mid + 1, right, key); } } return -1;

Logorithms (inverse of exponents) What is 965 * 2,974 = ???? What is 1,000 * 10,000 = 10,000,000 1) 𝑙𝑜𝑔 10 1,000 =3  1,000 = 10 ? 2) 𝑙𝑜𝑔 10 10,000 =4  10,000 =10 ? 3) 3 + 4 = 7 4) 𝑙𝑜𝑔 10 −1 ( 7 ) = 10,000,000  10 7 = ?

Repeatedly in half (NCAA bracket)

How many rounds until a champion If the NCAA tournament starts with 64 teams, and each round half the teams are eliminalted, how many rounds will there be until the one final team is left (champion)? 𝑙𝑜𝑔 2 64 =6  64 = 2 ? = 2*2*2*2*2*2

Advantages of binary search Faster time when searching O(log N ) vs. O(N) Linear search (unsorted values) O(N) Binary search T(n) = c + T( 𝑛 2 ) = c + c + T( 𝑛 4 ) = c + c + c + T( 𝑛 8 ) = c + … + c + T( 𝑛 𝑛 ) = ( 𝑙𝑜𝑔 2 n - 1 )* c + T(1) = ( 𝑙𝑜𝑔 2 n ) * c

Example: Facebook has over 2B users Suppose you wanted to login to Facebook and Facebook needed to search 2.38 billion users to find the userid you entered. A linear search will take 2.38 billion comparisons to search every userid. A binary search will take 𝑙𝑜𝑔 2 (2,380,000,000) ≈ 32 comparisons.

Disadvantages of binary search In order to do a binary search, the elements must be maintained in a data structure that they can be searched using the binary search algorithm. 2 popular examples: An array in order A binary search tree The disadvantage is that the data structure must be maintained. Example: Every day facebook has new users join and existing users leave the site, this must all be maintained.

Disadvantages of binary search

Disadvantages of binary search Overkill for small datasets. Recursive calls are time consuming. Comparisons of < > are not needed in linear search. Only work on elements with < , >, == relationships.

Future Topics Binary Search Trees (BST) Inserting into a BST Deleting from a BST Balancing a BST