CMPT 120 Topic: Searching – Part 2

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Algorithm Efficiency and Sorting
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
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.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Analysis of Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
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.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
CMPT 120 Topic: Sorting Algorithms – Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n -> O(n) i.e., has.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
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.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Topic: Python Lists – Part 1
Topic: Programming Languages and their Evolution + Intro to Scratch
CMPT 120 Topic: Searching – Part 2
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Algorithms
CMPT 120 Topic: Searching – Part 1
Search Lesson Outline Searching Lesson Outline
Teach A level Computing: Algorithms and Data Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
i206: Lecture 8: Sorting and Analysis of Algorithms
CSC212 Data Structure - Section RS
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Standard Version of Starting Out with C++, 4th Edition
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Principles of Computing – UFCFA3-30-1
Data Structures Sorted Arrays
Data Structures: Searching
Data Structures Introduction
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSC212 Data Structure - Section KL
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
Topic: Iterative Statements – Part 2 -> for loop
CMPT 120 Lecture 29 – Unit 5 – Internet and Big Data
CMPT 120 Lecture 30 – Unit 5 – Internet and Big Data
CMPT 120 Lecture 32 – Unit 5 – Internet and Big Data
CMPT 120 Lecture 33 – Unit 5 – Internet and Big Data
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
CMPT 225 Lecture 7 – Stack.
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

CMPT 120 Topic: Searching – Part 2 and Intro to Time Efficiency (Algorithm Analysis)

Last Lecture Intro to time efficiency and the Big O notation Analyzed the time efficiency of Linear Search

Learning Outcome At the end of this course, a student is expected to: Create (design), analyze, and explain the behaviour of simple algorithms: … Describe and illustrate the operation of linear search, binary search, and O(n2) sorting algorithms Analyze the running time of simple iterative algorithms Compare the running time of algorithms; determine if one algorithm is more time efficient than another Create (design) small to medium size programs using Python: Create programs that search lists and strings

Today’s Menu We shall look at another searching algorithm

Lock-in Syndrome Activity

One possible algorithm -> binary search algorithm Question 1: does your word start with a letter <= M? Possible answer: Yes, so we can ignore ½ of the alphabet A B C D E F G H I J K L M N O P Q R S T U V W X Y Z No, so we can ignore the other ½

One possible algorithm -> binary search algorithm Next question: Question 2: does your word start with a letter <= G? Possible answer: Yes, so we can ignore ½ of the alphabet A B C D E F G H I J K L M No, so we can ignore the other ½ OR Question 2: does your word start with a letter <= T? N O P Q R S T U V W X Y Z

One possible algorithm -> binary search algorithm Next question: Question 3: does your word start with a letter <= D? Possible answer: Yes, so we can ignore ½ of the alphabet A B C D E F G No, so we can ignore ½ of the alphabet OR Question 3: does your word start with a letter <= J? H I J K L M Question 3: does your word start with a letter <= Q? N O P Q R S T Question 3: does your word start with a letter <= W? U V W X Y Z etc...

Another example Suppose we have a sorted list 1 3 4 7 9 11 12 14 21 Using Binary Search algorithm, we can search for target = 7 without having to look at every element 1 3 4 7 9 11 12 14 21

How binary search works – In a nutshell Binary search uses the fact that the list is sorted! Find element in the middle -> 9 Since we are looking for 7 and 7 < 9, then there is no need to search the second half of the list We can ignore half of the list right away! Then we repeat the above steps Bottom line: using binary search, we do not need to look at every element to search a list

Animation: How it works – 1 We start with a list and a target = 7 1 3 4 7 9 11 12 14 21 We find the middle element 1 3 4 7 9 11 12 14 21 Is this element == target? Yes, then we are done! No, then we throw away half of the list in which we know target cannot be located (grey part) 1 3 4 7 9 11 12 14 21 and we consider only the part in which target could be located We repeat steps 2 and 3 until we found target or run out of list.

Animation: How it works – 2 We find the middle element 1 3 4 7 9 11 12 14 21 Is this element == target? Yes, then we are done! No, then we throw away half of the list in which we know target cannot be located (grey part) 1 3 4 7 9 11 12 14 21 and we consider only the part in which target could be located We repeat steps 2 and 3 until we found target or run out of list.

Animation: How it works - 3 We find the middle element 1 3 4 7 9 11 12 14 21 Is this element == target? Yes, then we are done! No, then we throw away half of the list in which we know target cannot be located (grey part) 1 3 4 7 9 11 12 14 21 and we consider only the part in which target could be located We repeat steps 2 and 3 until we found target or run out of list.

Animation: How it works - 4 We find the middle element 1 3 4 7 9 11 12 14 21 Is this element == target? Yes, then we are done! 

Binary Search algorithm - iterative PreCondition: data must be sorted binarySearch(list, target) set position to value TARGET_NOT_FOUND set targetNotFound to value true if list not empty while targetNotFound AND have not looked or discarded every element of list find middle element of list if middle element == target set position to position of target in original list set targetNotFound to false else if target < middle element list = first half of list list = last half of list return position We ignore 2nd half of the list and middle element We ignore 1st half of the list and middle element

Binary Search - Advantages and disadvantages Faster because does not have to look at every element (at every iteration, ignores ½ of list) Disadvantages List must be sorted A bit more complicated to implement and test

Question: BTW, can we quickly tell if target is not in a list?

Time efficiency of binary search algorithm How long did it take Mr. Bauby to write his book? Answer: time it took to guess one letter of a word multiply by average length of a word in English (or French) multiply by number of words in JD Bauby’s book But, for now, let’s focus on how long it takes to find the target in list using the binary search algorithm? 

Time efficiency of binary search algorithm Question: What is the time efficiency of the worst case scenario of the binary search algorithm? Answer: What are the worst case scenarios? What are the “critical operations”? Now, let’s analyze binary search algorithm Figure out count (# of times critical operations are performed) Express count as a f(n) Match count (as a f(n)) with one of the Big O notations: O(1), O(log n), O(n), O(n2), …

Step 1: Figure out count by counting # of times we do the critical operations Test Case: iteration count: ourList = [1, 3, 4, 7, 9, 11, 12, 14, 21] target = 16 ourList = [1, 3, 4, 7, 9, 11, 12, 14, 21] target = 7 == count: n: 9

Step 2 – Express count as a f(n) How does count relates to n? Using our example, how does count = 4 relates to n = 9?

Step 2 – Express count as a f(n) Binary Search Algorithm At each iteration: We compare the middle element with target and if target not found We partition the list in half, ignoring one half and searching the other Size of data in 1st iteration: n Size of data in 2nd iteration : n/2 D Size of data in 3rd iteration : n/4 Size of data in Tth iteration : 1

Step 2 – Express count as a f(n) Note, on this slide, N is used instead of n. D

Step 3 - Match count as a f(n) with … … one of the Big O notations: O(1), O(log2 n), O(n) or O(n2)?

Time efficiency of binary search algorithm Result of binary search algorithm efficiency analysis: The worst case scenario of the binary search algorithm is of order log2 n i.e., has a time efficiency of O(log2 n) since the time required (i.e., number of times the critical operation is executed) by the binary search algorithm (under the worst case scenario) to find “target” in a list of length n is proportional to the log of the number of elements in the list, i.e., n

Comparing binary search to linear search How much “faster” is binary search over linear search as n goes to infinity? To answer this question, let’s have a look at the diagram on next slide

Big O notation O(n2) O(n) O(log2 n) O(1) # of times algorithm executes a “critical operation” O(log2 n) O(1) 1 Length/size of data As n goes to infinity

Comparing binary search to linear search Binary search is much faster than linear search when searching large data But the data must first be sorted Great if data is already sorted, but if this is not the case … How much work does this sorting requires?

Summary Looked at the second way of searching Binary search algorithm Compare and contrast linear and binary searching algorithms

Next Lecture Sorting algorithms and their time efficiency