CSC212 Data Structure - Section RS

Slides:



Advertisements
Similar presentations
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Advertisements

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.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Searching Arrays Linear search Binary search small arrays
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
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.
Abstract Data Types (ADTs) Data Structures The Java Collections API
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.
ITEC 2620A Introduction to Data Structures
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.
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 م. م علي عبد الكريم حبيب.
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.
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.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Using recursion for Searching and Sorting
Searching and Sorting Arrays
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Searching and Sorting Arrays
Data Structures and Algorithms for Information Processing
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
CSC212 Data Structure - Section AB
Chapter 9: Searching, Sorting, and Algorithm Analysis
Introduction to complexity
COMP 53 – Week Seven Big O Sorting.
Data Structures and Algorithms for Information Processing
COSC160: Data Structures Linked Lists
Algorithm Analysis CSE 2011 Winter September 2018.
Search by Hashing.
Teach A level Computing: Algorithms and Data Structures
Topic 14 Searching and Simple Sorts
CSc 110, Spring 2017 Lecture 39: searching.
CSC212 Data Structure - Section RS
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
Searching CLRS, Sections 9.1 – 9.3.
ITEC 2620M Introduction to Data Structures
Topic 14 Searching and Simple Sorts
COMPUTER 2430 Object Oriented Programming and Data Structures I
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Searching and Sorting Arrays
CSC212 Data Structure - Section KL
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Searching.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

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

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

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

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

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) found = true; else ++i; } if (found) return i; // indicating the location of the desired item return –1; // indicating “not found” A serial search algorithm steps through (part of ) an array one item a time, looking for a “desired item”

Serial Search -Analysis 3 2 4 6 5 1 8 7 Size of array: n Best-Case: O(1) item in [0] Worst-Case: O(n) item in [n-1] or not found Average-Case usually requires fewer than n array accesses But, what are the average accesses? 3 6 7 9

Average-Case Time for Serial Search A more accurate computation: Assume the target to be searched is in the array and the probability of the item being in any array location is the same The average accesses denominator: the part of a fraction that is below the line 1/n is the probability that an item in location i, i=0,...n-1 numerator : the part of the fraction that is above the line

When does the best-case time make more sense? For an array of n elements, the best-case time for serial search is just one array access. The best-case time is more useful if the probability of the target being in the [0] location is the highest. or loosely if the target is most likely in the front part of the array So this tell us that the knowledge of the locations of the target is helpful

Binary Search If n is huge, and the item to be searched can be in any locations, serial search is slow on average But if the items in an array are sorted, we can somehow know a target’s location earlier Array of integers from smallest to largest Array of strings sorted alphabetically (e.g. dictionary) Array of students records sorted by ID numbers Example: Look up a word in a dictionary e.g. store first turn to s section, then t sub-section, so on and so forth

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

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

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

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

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

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

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

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

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

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

Binary Search Code 6 parameters 2 stopping 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); 6 parameters 2 stopping cases 2 recursive call cases

Binary Search - Analysis 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); Analysis of recursive algorithms Analyze the worst-case Assuming the target is in the array and we always go to the second half

Binary Search - Analysis 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 Analysis of recursive algorithms Define T(n) is the total operations when size=n T(n) = 6+T(n/2) T(1) = 6

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

Worst-Case Time for Binary Search For an array of n elements, the worst-case time for binary search is logarithmic We have given a rigorous proof The binary search algorithm is very efficient What is the average running time? The average running time for actually finding a number is O(log n) Can we do a rigorous analysis???? 6(m+1)-> 6m 6(1+2+...+m)/ m = 6m(m+1)/2m =3(m+1) -> log(n)

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

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