@ Zhigang Zhu, 2002-2014 1 CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
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.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Zhigang Zhu Department.
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Recursion. Binary search example postponed to end of lecture.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Recursion.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Searching Arrays Linear search Binary search small arrays
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
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.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
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.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Forensics and CS Philip Chan. CSI: Crime Scene Investigation high tech forensics tools DNA profiling Use.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
1 Searching and Sorting Linear Search Binary Search.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Searching CS 105 See Section 14.6 of Horstmann text.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 9: Searching Data Structures.
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.
Forensics and CS Philip Chan. CSI: Crime Scene Investigation high tech forensics tools DNA profiling Use.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
Design and Analysis of Algorithms & Computational Complexity CS490 Koji Tajii.
Searching CS 110: Data Structures and Algorithms First Semester,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-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, ]
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 Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Data Structures and Algorithms for Information Processing
Search by Hashing.
CSC212 Data Structure - Section RS
CSC212 Data Structure - Section RS
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
COMPUTER 2430 Object Oriented Programming and Data Structures I
Searching and Sorting Arrays
CSC212 Data Structure - Section KL
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College of New York

@ Zhigang Zhu, Topics p Applications p Most Common Methods p Serial Search p Binary Search p Search by Hashing (next lecture) p Run-Time Analysis p Average-time analysis p Time analysis of recursive algorithms

@ Zhigang Zhu, Applications p Searching a list of values is a common computational task p Examples p database: student record, bank account record, credit record... p Internet – information retrieval: Yahoo, Google p Biometrics –face/ fingerprint/ iris IDs

@ Zhigang Zhu, Most Common Methods p Serial Search p simplest, O(n) p Binary Search p average-case O(log n) p Search by Hashing (the next lecture) p better average-case performance

@ Zhigang Zhu, Serial Search p A serial search algorithm steps through (part of ) an array one item a time, looking for a “desired item” Pseudocode for Serial Search Pseudocode for Serial Search // search for a desired item in an array a of size n set i to 0 and set found to false; while (i<n && ! found) { if (a[i] is the desired item) if (a[i] is the desired item) found = true; found = true; else else ++i; ++i;} if (found) return i; // indicating the location of the desired item return i; // indicating the location of the desired itemelse return –1; // indicating “not found” return –1; // indicating “not found”

@ Zhigang Zhu, Serial Search -Analysis p Size of array: n p Best-Case: O(1) p item in [0] p Worst-Case: O(n) p item in [n-1] or not found p Average-Case p usually requires fewer than n array accesses p But, what are the average accesses?

@ Zhigang Zhu, Average-Case Time for Serial Search p A more accurate computation: p Assume the target to be searched is in the array p and the probability of the item being in any array location is the same p The average accesses

@ Zhigang Zhu, When does the best-case time make more sense? p For an array of n elements, the best-case time for serial search is just one array access. p The best-case time is more useful if the probability of the target being in the [0] location is the highest. p or loosely if the target is most likely in the front part of the array

@ Zhigang Zhu, Binary Search p If n is huge, and the item to be searched can be in any locations, serial search is slow on average p But if the items in an array are sorted, we can somehow know a target’s location earlier p Array of integers from smallest to largest p Array of strings sorted alphabetically (e.g. dictionary) p Array of students records sorted by ID numbers

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 16 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] if target is in the array

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 16 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] if target is in the array

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 16 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] DONE if target is in the array

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 16 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] DONE if target is in the array recursive calls: what are the parameters?

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 16 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] DONE if target is in the array recursive calls with parameters: array, start, size, target found, location // reference

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 17 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] if target is not in the array

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 17 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] if target is not in the array [0] [0]

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 17 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] if target is not in the array [0] [0]

@ Zhigang Zhu, Binary Search in an Integer Array p Items are sorted p target = 17 p n = 8 p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] if target is not in the array [0] [0] the size of the first half is 0!

@ Zhigang Zhu, Binary Search in an Integer Array p target = 17 p If (n == 0 ) p not found! p Go to the middle location i = n/2 p if (a[i] is target) p done! p else if (target <a[i]) p go to the first half p else if (target >a[i]) p go to the second half [0] [1] [2] [3] [4] [5] [6] [7] [0] [1] [2] [3] [0] [1] [2] if target is not in the array [0] [0] the size of the first half is 0!

@ Zhigang Zhu, Binary Search Code p 6 parameters p 2 stopping cases p 2 recursive call cases void search (const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location) { size_t middle; if (size == 0) // stopping case if not found found = false; else { middle = first + size/2; if (target == a[middle]) // stopping case if found { location = middle; found = true; } else if (target < a[middle]) // search the first half search(a, first, size/2, target, found, location); else //search the second half search(a, middle+1, (size-1)/2, target, found, location); }

@ Zhigang Zhu, Binary Search - Analysis p Analysis of recursive algorithms p Analyze the worst-case p Assuming the target is in the array p and we always go to the second half void search (const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location) { size_t middle; if (size == 0) // stopping case if not found found = false; else { middle = first + size/2; if (target == a[middle]) // stopping case if found { location = middle; found = true; } else if (target < a[middle]) // search the first half search(a, first, size/2, target, found, location); else //search the second half search(a, middle+1, (size-1)/2, target, found, location); }

@ Zhigang Zhu, Binary Search - Analysis p Analysis of recursive algorithms p Define T(n) is the total operations when size=n T(n) = 6+T(n/2) T(1) = 6 void search (const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location) { size_t middle; if (size == 0) // 1 operation found = false; else { middle = first + size/2; // 1 operation if (target == a[middle]) // 2 operations { location = middle; // 1 operation found = true; // 1 operation } else if (target < a[middle]) // 2 operations search(a, first, size/2, target, found, location); else // T(n/2) operations for the recursive call search(a, middle+1, (size-1)/2, target, found, location); } // ignore the operations in parameter passing }

@ Zhigang Zhu, Binary Search - Analysis p How many recursive calls for the longest chain? original call 1st recursion, 1 six 2nd recursion, 2 six mth recursion, m six and n/2 m = 1 – target found depth of the recursive call m = log 2 n

@ Zhigang Zhu, Worst-Case Time for Binary Search p For an array of n elements, the worst-case time for binary search is logarithmic p We have given a rigorous proof p The binary search algorithm is very efficient p What is the average running time? p The average running time for actually finding a number is O(log n) p Can we do a rigorous analysis????

@ Zhigang Zhu, Summary p Most Common Search Methods p Serial Search – O(n) p Binary Search – O (log n ) p Search by Hashing (*) – better average-case performance ( next lecture) p Run-Time Analysis p Average-time analysis p Time analysis of recursive algorithms

@ Zhigang Zhu, Homework p Review Chapters 10 & 11 (Trees), and p do the self_test exercises – for p Exam 3 p Read Chapters 12 & 13, and p do the self_test exercises – for Exam 3 p Homework/Quiz (on Searching): p Self-Test 12.7, p 590 (binary search re-coding) p Turn in on Dec 03 on paper (please print)