Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
MATH 224 – Discrete Mathematics
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
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Chapter 19: Searching and Sorting Algorithms
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Recursion … just in case you didn’t love loops enough …
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
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.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Chapter 8 ARRAYS Continued
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
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.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
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.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Searching and Sorting Arrays
Introduction to complexity
Algorithm design and Analysis
CSC212 Data Structure - Section RS
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching.
Searching and Sorting Arrays
CSC212 Data Structure - Section KL
Presentation transcript:

searching1 Searching The truth is out there...

searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the item is found –all items have been examined Algorithm is easy to code and works OK for small data sets

searching3 Code example for serial search // precondition: none // postcondition: searches an array of N items for target value: // returns true if target found, false if not template bool SerialSearch (item array[], size_t N, item target) { bool found = false; for (size_t x=0; (x < N) && (!found); x++) if (array[x] == target) found = true; return found; }

searching4 Time analysis of serial search Worst case: serial search is O(N) -- if item not found, have to go through whole array before this can be verified Best case: O(1) -- target value found at array[0] Average case: O((N+1)/2) -- basically still O(N), but about 1/2 the time required for worst case

searching5 Binary search Much faster than serial search Works only if data are sorted Uses divide & conquer approach with recursive calls: –check value at midpoint; if not target then –if greater than target, make recursive call to search “upper” half of structure –if less than target, recursively search “lower” half

searching6 Implementation of binary search // precondition: none // postcondition: searches an array of N items for target value: // returns true if target found, false if not template void BinarySearch(item array[], size_t first, size_t size, item target, bool& found, size_t& location) // parameters: array is the array to be searched, //first is the first index to be considered, //size is the number of items in search group //target is the value being sought, //found is the success/failure flag //location is the index of the entry containing the //target value, if found

searching7 Binary search code continued { // start of function size_t middle; // index of midpoint of current search area if (size == 0) found = false;// base case else { middle = first + size / 2; if (target == array[middle]) { location = middle; found = true; }

searching8 Binary search code continued // target not found at current midpoint -- search appropriate half else if (target < array[middle]) BinarySearch (array, first, size/2, target, found, location); // searches from start of array to index before midpoint else BinarySearch (array, middle+1, (size-1)/2, target, found, location); // searches from index after midpoint to end of array } // ends outer else } // ends function

searching9 Binary search in action Suppose you have a 13-member array of sorted numbers: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Searching for value: 113 Initial function call:first = 0, size = 13, middle = 6

searching10 Binary search in action [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Searching for value: 113 Initial function call:first = 0, size = 13, middle = 6 Since 113 != 82, make recursive call: BinarySearch (array, middle+1, (size-1)/2, target, found, location);

searching11 Binary search in action [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Searching for value: 113 Recursive call(1):first = 7, size = 6, middle = 10

searching12 Binary search in action [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Searching for value: 113 Recursive call(1):first = 7, size = 6, middle = 10 Since 113 != 130, make recursive call: BinarySearch(array, first, size/2, target, found, location);

searching13 Binary search in action [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Searching for value: 113 Recursive call(2):first = 7, size = 3, middle = 8

searching14 Binary search in action [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Searching for value: 113 Recursive call(2):first = 7, size = 3, middle = 8 Since 113 != 108, make recursive call: BinarySearch(array, middle+1, (size+1)/2, target, found, location);

searching15 Binary search in action [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Searching for value: 113 Recursive call(3):first = 9, size = 1, middle = 9

searching16 Binary search in action [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] Searching for value: 113 Recursive call(3):first = 9, size = 1, middle = 9 Since 113 == 113, target is found; found = true, location = 9

searching17 Binary Search Analysis Worst-case scenario: item is not in the array –algorithm keeps searching smaller subarrays –eventually, array size will be 0, and the search will stop Analysis requires computing time needed for operations in function as well as amount of time for recursive calls We will analyze the algorithm’s performance in the worst case

searching18 Step 1: count operations Test base case: if (size==0) 1 operation Compute midpoint: middle = first + size/2; 3 operations Test for target at midpoint: if (target == array[middle])2 operations Test for which recursive call to make: if (target < array[middle])2 operations Recursive call - requires some arithmetic and argument passing - estimate 10 operations

searching19 Step 2: analyze cost of recursion Each recursive call is preceded by 18 (or fewer) operations Multiply this number by the depth of recursive calls and add the number of operations performed in the stopping case to determine worst-case running time (T(n)) T(n) = 18 * depth of recursion + 3

searching20 Step 3: estimate depth of recursion Calculate upper bound approximation for depth of recursion; may slightly overestimate, but will not underestimate actual value –Each recursive call is made on an array segment that contains, at most, N/2 elements –Subsequent calls are always made on size/2 –Thus, depth of recursion is, at most, the number of times N can be divided by 2 with a result > 1

searching21 Estimating depth of recursion Referring to “the number of times N is divisible by 2 with result > 1” as H(n), or the halving function, the time expression becomes: T(n) = 18 * H(n) + 3 H(n) turns out to be almost exactly equal to log2n: H(n) = log2n meaning that fractional results are rounded down to the nearest whole number (e.g. 3.7 = 3) -- this notation is called the floor function

searching22 Worst-case time for binary search Substituting the floor function of the logarithm for H(n), the time expression becomes: T(n) = 18 * ( log 2 n ) + 3 Throwing out the constants, the worst-case running time (big O) function is: O(log n)

searching23 Significance of logarithms (again) Logarithmic algorithms are very fast because log n is much smaller than n The larger the data set, the more dramatic the difference becomes: –log 2 8 = 3 –log 2 64 = 6 –log < 10 –log 2 1,000,000 < 20

searching24 For binary search algorithm... To search a 1000 element array will require no more than 183 operations in the worst case To search a 1,000,000 element array will require less than 400 operations in the worst case