Searching Topics Sequential Search Binary Search.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

CSE Lecture 3 – Algorithms I
Analysis of Algorithms CS Data Structures Section 2.6.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Fundamentals of Python: From First Programs Through Data Structures
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Cmpt-225 Algorithm Efficiency.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Analysis of Algorithm.
Chapter 3: The Efficiency of Algorithms
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
COMP s1 Computing 2 Complexity
Week 2 CS 361: Advanced Data Structures and Algorithms
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Searching and Sorting Topics Sequential Search on an Unordered File
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
© 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.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
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.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
3.3 Complexity of Algorithms
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Sorting.
Algorithm Analysis (Big O)
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
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.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
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.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 16: Searching, Sorting, and the vector Type.
1 Algorithms Searching and Sorting Algorithm Efficiency.
1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
Algorithmic Efficency
Introduction to Search Algorithms
Sorting by Tammy Bailey
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Topics Sequential Search on an Unordered File
Manipulating lists of data
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Analysis of Algorithms
Analysis of Algorithms
Analysis of Algorithms
Presentation transcript:

Searching Topics Sequential Search Binary Search

Search Algorithms The most important operation on a list is the search algorithm Using a search algorithm you can: –Determine whether a particular item is in the list –If the data are specially organized, find the location where a new item can be inserted –Find the location of an item to be deleted The search algorithm’s performance (search time) is crucial

Searching A question you should always ask when selecting a search algorithm is “How fast does the search have to be?” The reason is that, in general, the faster the algorithm is, the more complex it is. Bottom line: you don’t always need to use or should use the fastest algorithm. Let’s explore the following search algorithms, keeping speed in mind. –Sequential (linear) search –Binary search

Sequential Search Given the following list of integers, search for 35 : 2, 3, 67, 34, 9, 5, 12, 47, 35, 90 How did you do it?

The Sequential Search Always starts at the first element in the list Continues until either the item is found in the list or the entire list is searched –If found, its index is returned –If not found, -1 is returned To analyze a search algorithm, count the number of key comparisons

Running Time Analysis Best Case: search item is the first element in the list –The algorithm makes one comparison Worst Case: search item is the last element in the list –The algorithm makes n comparisons, where n is the number of elements in the list

Running Time Analysis Best and worst cases are not likely to occur. To determine the average number of comparisons for the successful case of the sequential search algorithm: –Consider all possible cases –Find the number of comparisons for each case –Add the number of comparisons and divide by the number of cases

Running Time Analysis Assuming n elements in the list, the following expression gives the average number of comparisons 1+2+…+n n It is known that 1+2+…+n = n(n+1) 2

Running Time Analysis The average number of comparisons in the successful case is: 1+2+…+n = 1 n(n+1)= n+1 n n 2 2 On average the sequential search algorithm searches half the list Not good for large lists

Binary Search If we have an ordered list and we know how many things are in the list (i.e., number of records in a file), we can use a different strategy. The binary search gets its name because the algorithm continually divides the list into two parts.

Binary Search Given the following list of integers, search for 35: 2, 3, 5, 9, 12, 34, 35, 47, 67, 90 How would you do it?

How a Binary Search Works Always look at the center value. Each time you get to discard half of the remaining list. Is this fast ?

How Fast is a Binary Search? Worst case: 11 items in the list took 4 tries Worst case: 32 items… 1st try - list has 16 items 2nd try - list has 8 items 3rd try - list has 4 items 4th try - list has 2 items 5th try - list has 1 item

How Fast is a Binary Search? (con’t) List has 250 items 1st try items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item List has 512 items 1st try items 2nd try items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item

What’s the Pattern? List of 11 took 4 tries List of 32 took 5 tries 32 = 2 5 List of 250 took 8 tries List of 512 took 9 tries 512 = 2 9

A Very Fast Algorithm! How long (worst case) will it take to find an item in a list 30,000 items long? 2 10 = = = = = = So, it will take only 15 tries!

Binary Search Efficiency We say that the binary search algorithm runs in log 2 n time. (Also written as lg n) Lg n means the log to the base 2 of some value of n 8 = 2 3 lg 8 = 316 = 2 4 lg 16 = 4 There are no algorithms that run faster than lg n time.

Big O Notation Comparing algorithms  “Analysis of Algorithms” Order of Magnitude - O(…)

How do we compare algorithms? Comparing execution time  Varies depending on computer. Counting instructions  Varies depending on language & computer (machine code dependent). Count passes through critical sections of code  more standardized. (usually loops!)

BIG - O  Order of Magnitude Relatively how big is the task Base value on part that increases in size the quickest as N increases. An approximation, not exact Other terms do not contribute in computing time when N is large.

Common Orders of magnitude O(1) - bounded time (# elements doesn’t matter) (unsorted Insert( )) O(N) - linear time (unsorted IsPresent( ), Delete( ), sorted Insert( )) (Amount of work is some constant times the number of elements) O(log 2 N) - logarithmic time - problem splits with each pass (binary search) O(N log 2 N) - N log 2 N time - Applying a logarithmic algorithm N times. (good sorts) O(N 2 ) - quadratic time - Applying a linear algorithm N times (selection sort, insertion sort)

Algorithm Tradeoffs Compare using Big-O Examine elegance Examine efficiency of memory use Examine ease of modification