Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.

Slides:



Advertisements
Similar presentations
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Advertisements

CSE Lecture 3 – Algorithms I
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
Data Structures Using Java1 Chapter 8 Search Algorithms.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Searching Arrays Linear search Binary search small arrays
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Search - CIS 1068 Program Design and Abstraction
Data Structures Using Java1 Chapter 8 Search Algorithms.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Search - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 19/23/2015.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
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.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
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.
1 Searching and Sorting Linear Search Binary Search.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
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.
IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms.
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.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
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.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
Searching Damian Gordon. Google PageRank Damian Gordon.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Data Structure Introduction.
Sorting Sorting: –Task of rearranging data in an order. –Order can be: Ascending Order: –1,2,3,4,5,6,7,8,9 Descending Order: –9,8,7,6,5,4,3,2,1 Lexicographic.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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.
Algorithm Analysis Chapter 5. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. –recipes –directions.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
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.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
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.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Searching CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
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)
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
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 and Sorting Arrays
24 Searching and Sorting.
Cs212: DataStructures Lecture 3: Searching.
Data Structures: Searching
Presentation transcript:

Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information should be as quick as possible which can be only achieved if, –The number of comparisons are as minimum as possible.

Searching Classification Tree Search Non-Linear Search Sequential Search Binary Search Linear Search Graph Search Array Search Linked List Search Binary Tree Search AVL Tree Search

Sequential Search on Array Array A Index 9 i = 0 While (i <= 4) Process (A[ i ]) i = i + 1 EndWhile Steps: If( A[ i ] == 9 ), then EndIf found = 1 location = i, found = 0, location = NULL If (found == 0), then Else EndIf Return(location) print “Search is unsuccessful.” print “Search is successful.” && (found == 0), do Question: Will it work for any array? i = L (i <= U) Question: Will it work for any search value? KEY = If (A[ i ] == KEY) Question: Is it the most optimized / efficient? L U

Sequential Search on Array Algorithm: –SequentialSearch. Input: –Array A with elements. –KEY: Element to be searched. Output: –On Success, appropriate message and return Index/Location of KEY in array A. –On Failure, appropriate message and return NULL. Data Structure: –Array A[L...U]

Algorithm: SequentialSearch Steps: i = L, found = 0, location = NULL While (i <= U) && (found == 0), do If( A[ i ] == KEY ), then found = 1 location = i EndIf i = i + 1 EndWhile If (found == 0), then print “Search is unsuccessful.” Else print “Search is successful.” EndIf Return(location) Stop

Analysis of Sequential Search Complexity Analysis: –Performance of any search algorithm depends on: Number of comparisons it has to do. –The lesser the comparisons, »Better is the performance. –The greater the number of comparisons, »Slower is the performance. –3 different cases: Case-1: –KEY matches with the first element. »Best case. Case-2: –KEY does not exist OR KEY matches with the last element. »Worse case. Case-3: –KEY is present at any location in the array. »Average case.

Analysis of Sequential Search Complexity Analysis: –Best Case: KEY is present in the array as the 1 st element. We will need only 1 comparison for searching. –Number of comparisons: »T(n) = 1.

Analysis of Sequential Search Complexity Analysis: –Worse Case: KEY does not exist OR KEY matches with the last element in the array. If the size of the array is n, –Then minimum number of comparisons needed to reach any conclusion is: »T(n) = n

Analysis of Sequential Search Complexity Analysis: –Average Case: KEY is present at any location in the array. Let p i be the probability that the key may be present at the i-th location. (1 <= i <= n) –Number of comparisons, i = 1 i = n p i * iT(n) =

Analysis of Sequential Search i = 1 i = n p i * iT(n) = For simplicity, let us assume that place of key is equally probable at every location. So p1 = p2 = p3 = p4 =... pn = 1/n Average Case i = 1 i = n iT(n) =(1/n) * (n (n+1) / 2)T(n) =(1/n) (n+1) / 2T(n) =

Analysis of Sequential Search Summary Case Number of Key Comparisons T(n) Best CaseT(n) = 1 Worse CaseT(n) = n Average CaseT(n) = (n+1) / 2

Sequential Search on Array KEY = L U Array A What if the array elements are already sorted? KEY = L U Array A Searching stops here. Searching stops here.

Searching Use of Sequential Search: –Works fine if the list is small. –Example: Find a name in a list of names of registered students. –Question: Will sequential search be efficient if you need to search the word ‘symbiosis’ in the dictionary? –Yes. »If you want to pass your time! Answer is obviously, –‘No’.

Searching Sequential Search: –Conclusion: If the array / list is not sorted: –No option. –We need to go for sequential searching. If the array is sorted: –Sequential search is not efficient. –We need to go for some other way of searching. »Binary Search.

Binary Search Array A LU KEY = 55 Index (Page No.) We need to go to mid / middle of the array. Question: How to calculate mid of the array because index does not start from 1? mid = 10 / 2 = 5Does not work. mid = 16 / 2 = 8Does not work. mid = (10+16) / 2 = 13Works. mid = (L+U) / 2 Question: This works because there are odd number of elements. What if there are even number of elements in the array mid = (10+17) / 2 = 13.5 mid = (L+U) / 2mid = floor(13.5) = 13

Binary Search Array A LU KEY = 45 Index (Page No.) mid = floor((L+U)/2)) Is A[mid] = KEY? Yes. Searching stops here. mid mid = floor((10+16)/2) mid = floor(26/2) mid = 13

Binary Search Array A LU KEY = 65 Index (Page No.) mid = floor((L+U)/2)) = floor((10+16)/2) = floor(26/2) = 13 Is A[mid] = KEY? No.Is KEY > A[mid] OR KEY < A[mid] mid = floor((L+U)/2)) = floor((14+16)/2) = floor(30/2) = 15 KEY > A[mid] mid L = mid + 1

Binary Search Array A LU KEY = 25 Index (Page No.) mid = floor((L+U)/2)) = floor((10+16)/2) = floor(26/2) = 13 Is A[mid] = KEY? No.Is KEY > A[mid] OR KEY < A[mid] mid = floor((L+U)/2)) = floor((10+12)/2) = floor(22/2) = 11 KEY < A[mid] mid U = mid - 1

Binary Search Array A LU KEY = 55 Index (Page No.) mid = floor((L+U)/2)) = floor((10+16)/2) = floor(26/2) = 13 mid = floor((L+U)/2)) = floor((14+16)/2) = floor(30/2) = 15 mid mid = floor((L+U)/2)) = floor((14+14)/2) = floor(28/2) = 14 mid

Binary Search Array A LU KEY = 40 Index (Page No.) mid = floor((L+U)/2)) = floor((10+16)/2) = floor(26/2) = 13 mid = floor((L+U)/2)) = floor((10+12)/2) = floor(22/2) = 11 mid mid = floor((L+U)/2)) = floor((12+12)/2) = floor(24/2) = 12 mid UL L KEY not found

Binary Search Algorithm: –BinarySearch Input: –KEY: Value to be searched. Output: –LOCATION: Index of the KEY if successful, otherwise NULL. Data Structure: –Array A[L...U] sorted in Ascending order.

Steps of BinarySearch found = 0, location = NULL While(L <= U) AND (found = 0), do mid = floor((L+U)/2) If(KEY = A[mid]), then found = 1 location = mid Else If(KEY > A[mid]), then L = mid + 1 Else U = mid – 1 EndIf EndWhile If(found = 0) print “Search Unsuccessful” Else print “Search Successful” EndIf Return(location) Stop

Tracing of Binary Search Array A Index LU 95 8 KEY = 75 L = 1 U = L U 95 8 L = 1 U = 8 mid = (floor(1+8)/2) = 4 KEY > A[4] L = mid + 1 = = 5 L = 5 U = 8 mid = (floor(5+8)/2) = 6 KEY = A[6] found = 1 location = L U 95 8 Input Iteration-1 Iteration-2 mid Search Successful.

Tracing of Binary Search Array A Index LU 95 8 KEY = 55 L = 1 U = L U 95 8 L = 1 U = 8 mid = (floor(1+8)/2) = 4 KEY > A[4] L = mid + 1 = = 5 L = 5 U = 8 mid = (floor(5+8)/2) = 6 KEY < A[6] U = mid – 1 = 6 – 1 = LU 95 8 Input Iteration-1 Iteration-2 mid L = 5 U = 5 mid = (floor(5+5)/2) = 5 KEY < A[5] U = mid – 1 = 5 – 1 = LU 95 8 Iteration-3 mid Search Unsuccessful.

Analysis of Binary Search Summary Case Number of Key Comparisons T(n) Best CaseT(n) = 1 Worse CaseT(n) = log 2 n Average CaseT(n) = log 2 n

Sequential v/s Binary Search CaseSequential SearchBinary Search Best CaseT(n) = 1 Worse CaseT(n) = nT(n) = log 2 n Average CaseT(n) = (n+1) / 2T(n) = log 2 n