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.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CS0007: Introduction to Computer Programming Array Algorithms.
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.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Sorting Algorithms Bubble Sort Merge Sort Quick Sort Randomized Quick Sort.
Searching Arrays Linear search Binary search small arrays
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
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,
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
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 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.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
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.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
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.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
CSC 211 Data Structures Lecture 13
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Analysis of Bubble Sort and Loop Invariant
Data Structure Introduction.
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Binary Search Trees (BST)
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.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Data Structures Arrays and Lists Part 2 More List Operations.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
 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
Searching Given a collection and an element (key) to find… Output
Sorting.
Recitation 13 Searching and Sorting.
Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals.
Linear and Binary Search
Search,Sort,Recursion.
Linear Search Binary Search Tree
24 Searching and Sorting.
Lecture 16 Bubble Sort Merge Sort.
Search,Sort,Recursion.
Presentation transcript:

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 the collection in the search!

Searching Example (Linear Search)

Linear Search: A Simple Search A search traverses the collection until –The desired element is found –Or the collection is exhausted If the collection is ordered, I might not have to look at all elements –I can stop looking when I know the element cannot be in the collection.

Un-Ordered Iterative Array Search procedure Search(my_array Array, target Num) i Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search Scan the array

procedure Search(my_array Array, target Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13 Target data found

procedure Search(my_array isoftype in NumArrayType, target isoftype in Num) i isoftype Num i <- 1 loop exitif((i > MAX) OR (my_array[i] = target)) i <- i + 1 endloop if(i > MAX) then print( “ Target data not found ” ) else print( “ Target data found ” ) endif endprocedure // Search my_array target = 13

Linear Search Analysis: Best Case Best Case: match with the first item target = 7 Best Case: 1 comparison

Linear Search Analysis: Worst Case Worst Case: match with the last item (or no match) target = 32 Worst Case: N comparisons

Searching Example (Binary Search on Sorted List)

The Scenario We have a sorted array We want to determine if a particular element is in the array –Once found, print or return (index, boolean, etc.) –If not found, indicate the element is not in the collection

A Better Search Algorithm Of course we could use our simpler search and traverse the array But we can use the fact that the array is sorted to our advantage This will allow us to reduce the number of comparisons

Binary Search Requires a sorted array or a binary search tree. Cuts the “search space” in half each time. Keeps cutting the search space in half until the target is found or has exhausted the all possible locations.

Binary Search Algorithm look at “ middle ” element if no match then look left (if need smaller) or right (if need larger) Look for 42

The Algorithm look at “ middle ” element if no match then look left or right Look for 42

The Algorithm look at “ middle ” element if no match then look left or right Look for 42

The Algorithm look at “ middle ” element if no match then look left or right Look for 42

The Binary Search Algorithm Return found or not found (true or false), so it should be a function. When move left or right, change the array boundaries –We’ll need a first and last

The Binary Search Algorithm calculate middle position if (first and last have “ crossed ” ) then “Item not found” elseif (element at middle = to_find) then “Item Found” elseif to_find < element at middle then Look to the left else Look to the right

Looking Left Use indices “first” and “last” to keep track of where we are looking Move left by setting last = middle – FLM L

Looking Right Use indices “first” and “last” to keep track of where we are looking Move right by setting first = middle FLM F

Binary Search Example – Found Looking for 42 FLM

Binary Search Example – Found Looking for 42 FLM

Binary Search Example – Found found – in 3 comparisons F L M

Binary Search Example – Not Found Looking for 89 FLM

Binary Search Example – Not Found Looking for 89 FLM

Binary Search Example – Not Found Looking for 89 FL M

Binary Search Example – Not Found not found – 3 comparisons FL

Function Find return boolean (A Array, first, last, to_find) middle <- (first + last) div 2 if (first > last) then return false elseif (A[middle] = to_find) then return true elseif (to_find < A[middle]) then return Find(A, first, middle–1, to_find) else return Find(A, middle+1, last, to_find) endfunction Binary Search Function

Binary Search Analysis: Best Case Best Case: match from the firs comparison Best Case: 1 comparison Target: 59

Binary Search Analysis: Worst Case Worst Case: divide until reach one item, or no match. How many comparisons??

Binary Search Analysis: Worst Case With each comparison we throw away ½ of the list N N/2 N/4 N/8 1 ………… 1 comparison ………… 1 comparison ………… 1 comparison ………… 1 comparison ………… 1 comparison Number of steps is at most  Log 2 N

Summary Binary search reduces the work by half at each comparison If array is not sorted  Linear Search –Best Case O(1) –Worst Case O(N) If array is sorted  Binary search –Best Case O(1) –Worst Case O(Log 2 N)