Searching.

Slides:



Advertisements
Similar presentations
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Advertisements

Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Searching Arrays Linear search Binary search small arrays
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Asymptotic Notations Iterative Algorithms and their analysis
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
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)
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
1 Searching and Sorting Linear Search Binary Search.
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.
Growth of Functions. Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime)
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching & Sorting. Algorithms Step by step recipe to do a task…
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Algorithm Analysis 1.
Advanced Sorting.
Using recursion for Searching and Sorting
Chapter 16: Searching, Sorting, and the vector Type
16 Searching and Sorting.
Analysis of Algorithms
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Searching – Linear and Binary Searches
Algorithmic Efficency
CS 3343: Analysis of Algorithms
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Sorting by Tammy Bailey
Sorting and Searching Sudeshna Sarkar 7th Feb 2017.
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.
Searching CSCE 121 J. Michael Moore.
Topic 14 Searching and Simple Sorts
CS 3343: Analysis of Algorithms
Building Java Programs
Adapted from Pearson Education, Inc.
Algorithm design and Analysis
Searching.
CSC215 Lecture Algorithms.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
searching Concept: Linear search Binary search
Searching & Sorting.
CSC 205 Java Programming II
MSIS 655 Advanced Business Applications Programming
Searching and Sorting 1-D Arrays
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Searching: linear & binary
Searching CLRS, Sections 9.1 – 9.3.
Linear Search Binary Search Tree
24 Searching and Sorting.
Algorithmic Complexity
Topic 14 Searching and Simple Sorts
8. Comparison of Algorithms
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Searching.
Presentation transcript:

Searching

Linear Search Linear Search Is it 1, 2, 3….

Linear Search Efficiency Linear Growth Best case: O(1) Worst case: O(n) Average: Check ~n/2 items = O(n)

Binary Search Binary Search Pick middle of remaining search space Too high? Eliminate middle and above Too low? Eliminate middle and below

Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4

Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4

Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower

Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower

Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 Found it!!!

Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 Found it!!!

Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher

Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower

Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower

Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 (value at location 3) too small, need to search higher

Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 (value at location 3) too small, need to search higher

Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 (value at location 3) too small, need to search higher 4 5 (one more than old middleLocation) 4 (unchanged) minLocation > maxLocation - we have nothing left to check - value is not there!

Binary Search Efficiency Each step cuts search space in half Algorithm: Step # Possible Unchecked Numbers 100 1 50 2 25 3 12 4 6 5 7

Binary Search Efficiency Logarithmic Growth Worst case = O(log2n) Num Items 10 50 100 500 1000 10000 100000 Binary Worst Case 4 6 7 9 14 17

Binary Average Half the items will always be worst case. Assume list with indexes 1-16: Step Locations That Might Be Checked 1 8 2 4, 12 3 2, 6, 10, 14 4 1, 3, 5, 7, 9, 11, 13, 15

Binary Average Half the items will always be worst case Even if other half are free: 𝑎𝑣𝑒𝑟𝑎𝑔𝑒= 𝑤𝑜𝑟𝑠𝑡 2 = 𝑙𝑜𝑔 2 𝑛 2 =𝑂( 𝑙𝑜𝑔 2 𝑛)

Comparison Not even close…

Recursive Binary Search Binary search recursive: BinarySearch(key, array, size) return recurseBinarySearch(key, array, 0, size - 1) recurseBinarySearch(key, array, low, high) if(low > high) return -1 //exhausted all possibilities mid = (low + high) / 2 if( array[mid] == key ) return mid else if( array[mid] > key ) return recurseBinarySearch(key, array, low, mid - 1) else return recurseBinarySearch(key, array, mid + 1, high)

Recursive Binary Search What is BigO for recursive??? Constant work Function call to self Only one BinarySearch(key, array, size) return recurseBinarySearch(key, array, 0, size - 1) recurseBinarySearch(key, array, low, high) if(low > high) return -1 //exhausted all possibilities mid = (low + high) / 2 if( array[mid] == key ) return mid else if( array[mid] > key ) return recurseBinarySearch(key, array, low, mid - 1) else return recurseBinarySearch(key, array, mid + 1, high)

Recursive Binary Search Recursion and BigO…

Recursive Binary Search Recursive cheat sheet: Binary search : Do constant work + time to solve half sized problem

Binary vs Linear Binary search requires sorted items How many searches do you need to do before it becomes worth sorting the items?

Find in Unsorted w/Linear Time Sort and Find w/Binary Time But… Can't sort a list in less than nlogn Linear search O(n), Binary search O(logn) Items Searched For Find in Unsorted w/Linear Time Sort and Find w/Binary Time 1 1*n nlogn + 1*logn 2 2*n nlogn + 2*logn … k k*n nlogn + k*logn

Break Even Break even curve for 𝑘𝑛=𝑛𝑙𝑜𝑔𝑛+𝑘𝑙𝑜𝑔𝑛