Hossain Shahriar Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Advertisements

Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Searching Arrays Linear search Binary search small arrays
Computer Science 111 Fundamentals of Programming I Search Algorithms.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Chapter 16: Searching, Sorting, and the vector Type.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Chapter 15 Recursion.
Applications of Arrays (Searching and Sorting) and Strings
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Lecture 12. Searching Algorithms and its analysis 1.
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.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
1 2. Program Construction in Java. 2.8 Searching.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
CSC 211 Data Structures Lecture 13
Hossain Shahriar Announcement and reminder! Final exam date December 19: 4pm-6pm Topics to be covered in this lecture Sort.
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 & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
September 26, 2011 Sorting and Searching Lists. Agenda Review quiz #3 Team assignment #1 due tonight  One per team Arcade game Searching  Linear  Binary.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 15 Recursion.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 16: Searching, Sorting, and the vector Type.
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.
Searching Arrays Linear search Binary search small arrays
Chapter 16: Searching, Sorting, and the vector Type
Binary Search.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Data Structures I (CPCS-204)
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Searching Given a collection and an element (key) to find… Output
Recitation 13 Searching and Sorting.
Section 2.6: Searching and Sorting
Chapter 7 Single-Dimensional Arrays
Search Algorithms Sequential Search (Linear Search) Binary Search
Searching CSCE 121 J. Michael Moore.
CSc 110, Spring 2017 Lecture 39: searching.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Searching and Sorting Arrays
Topic 1: Problem Solving
Linear Search Binary Search Tree
Cs212: DataStructures Lecture 3: Searching.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Last Class We Covered Recursion Stacks Parts of a recursive function:
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Presentation transcript:

Hossain Shahriar

Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19 December 20 December 21 Topics to be covered in this lecture Search

Find a specific element from a collection of object (e.g., number, string) We assume that the collection is sorted Two well known types Iterative search Binary search Iterative search We compare a key with each element of a collection and A for loop is required

Search (iterative) def iterative (collection, key): for i in range(len(collection)): if collection[i] == key: return True return False def main(): collection = [1, 3, 6, 8, 10] #list of numbers key = 18 #element to be searched found = iterative(collection, key) if found == True: print key, " found" else: print key, " not found" main()

Search (binary) Binary search We divide the collection into two equal parts Search either left or right part depending on the key value A while loop is required Algorithm Initialize low and up with lower and upper boundary of the list Get the middle element If the middle element equals to the key, then stop Otherwise If key is less than middle element, update low and up with low and mid+1, respectively If key is higher than middle element, update low and up with mid+1 and up, respectively

Search (binary) List = [1, 3, 5, 7, 9, 13, 24], key = 3 Key in lower half S0: low=0, up = 7 S1: mid=(low + up)/2=(0+7)/2=4, List[mid-1]=7, key=3, low=0, up =mid+1=5 S2: mid = (0+3)/2 = 1, List [mid-1] = 1, key =3, low=mid=1, up = 3 S3: mid = (1+3)/2 = 2, List [mid-1] = 3, key =3, match!!

Search (binary) List = [1, 3, 5, 7, 9, 13, 24], key = 13 Key in upper half S0: low=0, up = 7 S1: mid=(low + up)/2=(0+7)/2=3, List[mid-1]=5, key=13, low=3, up=7 S2: mid = (3+7)/2 = 5, List [mid-1] = 9, key =13, low=mid=5, up = 7 S3: mid = (5+7)/2 = 6, List [mid-1] = 13, key =13, match!!

Search (binary) List = [1, 3, 5, 7, 9, 13, 24], key = 100 Key not in the list S0: low=0, up = 7 S1: mid=(low + up)/2=(0+7)/2=3, List[mid-1]=5, key=100, low=3, up=7 S2: mid = (3+7)/2 = 5, List [mid-1] = 9, key =100, low=mid=5, up = 7 S3: mid = (5+7)/2 = 6, List [mid-1] = 13, key =100, low=mid=6, up =7 S4: mid = (6+7)/2 = 6, List [mid-1] = 13, key =100, low=mid=6, up =7 S5: mid = (6+7)/2 = 6, List [mid-1] = 13, key =100, low=mid=6, up =7 … oops!! we will stuck in the loop forever if a key is not in list Due to our round down operation We better do round up operation mid = int (math.ceil((low+up)/2.0))

Search (binary) def binary (collection, key): low=0 up = len(collection)-1 mid=0 while low < = up: mid = int(math.ceil((low+up)/2.0)) #print mid, low, up if collection[mid-1] == key: return True else: if key < collection[mid-1]: # the element could be in lower half up = mid +1 elif key >= collection[mid-1]: # the element could be in upper half low = mid +1 return False

Search (binary) Take home assignment # 4 Write a binary search algorithm Accept a list of string and a key string Return True or False, if the key is found or not Example: L = [‘have’, ‘I’, ‘just’, ‘learnt’, ‘searching’] Key=“have’, Binary(L, key) = True Key=“hello’, Binary(L, key) = False Hints: see how to compare two string

Quiz on Wednesday (Nov 23) String operator and function Modular programming Our last lecture will be on Sorting!!