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)

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
CSE Lecture 3 – Algorithms I
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:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
Elementary Data Structures and Algorithms
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.
C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
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.
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.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
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.
Arrays.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
PRESENTATION ON SEARCHING SEARCHING Searching is the method to find element from the list of the elements. If element is found then it.
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.
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.
CSC 211 Data Structures Lecture 13
Chapter 14: Searching and Sorting
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Sorting and Searching Bubble Sort Linear Search Binary Search.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 8A Binary Search (Concepts)
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 16: Searching, Sorting, and the vector Type.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
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
Lecture – 2 on Data structures
Searching CSCE 121 J. Michael Moore.
Linear and Binary Search
Searching.
searching Concept: Linear search Binary search
Searching and Sorting Arrays
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Searching: linear & binary
Searching CLRS, Sections 9.1 – 9.3.
Linear Search Binary Search Tree
Data Structures: Searching
Presentation transcript:

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) Linear Search (2) Binary Search

3 Linear Search A linear search is a technique for finding a particular value in a unsorted or sorted list. Search each item of list using key value, one at a time, until finding the item from the list. Algorithm: Linear-Search (L, N, Key) 1.Set k := 1 and Loc := 0 2.Repeat Steps 3 and 4 while k <= N 3. If Key = L[k], then Set Loc := k, print: Loc and exit. 4. Set k := k+1 5.If Loc = 0 then Write: Item is not in List 6.Exit Here L - The list of data items N – Total no. of items in L Key – Item to be searched.

4 Example of Linear Search List : 10, 6, 222, 44, 55, 77, 24 Key: 55 Steps: K= K= K= K= K=5 So, Item 55 is in position

5 Complexity Analysis of Linear Search (1)Worst Case The worst case occurs when Item is the last element in the List or is not in the List. So, C(n) = n = Θ(n). (2) Average Case The average case occurs when the availability of the Item in the List is equally likely to occur at any position in the List. So, C(n) = = = 0(n)

6 Binary Search List of items should be sorted. It uses the divide-and-conquer technique. The elements of the list are not necessarily all unique. If one searches for a value that occurs multiple times in the list, the index returned by the binary search will be of the first-encountered equal element. Compare the given Key value with the element in the middle of the list. It tells which half of the list contains the Item.Then compare Key with the element in the middle of the correct half to determine which quarter of the list contains Item. Continue the process until finding Item in the list.

7 Algorithm: Binary-Search (L, N, Key) 1.Set Loc := 0, Beg := 1, End :=N and Mid := (Beg + End)/2 2.Repeat steps 3 and 6 while Beg <= End 3. If Key < L[Mid], then Set End := Mid – 1 4. Else if Key>L[Mid] then Set Beg := Mid Else if Key = List[Mid] then Loc:= Key, print: Loc and exit. 6. Set Mid := (Beg + End)/2 7.If Loc = 0 Write: Item is not in List. 8.Exit. Here L - The list of data items N – Total no. of items in L Key – Item to be searched.

8 55 Example of Binary Search List : 6, 10, 20, 30, 55, 75, 190 Key: 55 Steps: Beg=1, End=7,Mid= Beg=5, End=7,Mid= Beg=5, End=5,Mid=5 [Item Found] So, Item 55 is in position

9 Complexity of Binary Search The complexity of binary search is measured by the number of comparisons to locate Item in List where List contains n elements. It is observed that each comparison reduces the sample size in half. So, C(n) = n + n/2 + n/4+…………..+1 = log 2 n+1 = 0(log 2 n)

10 END