1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
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.
Chapter 19: Searching and Sorting Algorithms
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
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.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
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.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
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 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
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.
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.
Lecture 12. Searching Algorithms and its analysis 1.
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.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
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.
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 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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 م. م علي عبد الكريم حبيب.
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.
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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 Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
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.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
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.
Data Structures Using C++
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.
Searching Topics Sequential Search Binary Search.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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 and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Searching Given a collection and an element (key) to find… Output
Search Algorithms Sequential Search (Linear Search) Binary Search
Searching CSCE 121 J. Michael Moore.
CSc 110, Spring 2017 Lecture 39: searching.
Searching and Sorting Arrays
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Presentation transcript:

1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University

Searching Arrays Searching is the process of looking for a specific element in an array; for example, discovering whether a certain score is included in a list of scores. Searching, like sorting, is a common task in computer programming. There are many algorithms and data structures devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search. 2

Linear Search The linear search approach compares the key element, key, with each element in the array list[]. The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the array that matches the key. If no match is found, the search returns -1. 3

4 Linear Search O (n) A Linear search of a list/array begins at the beginning of the list/array and continues until the item is found or the entire list/array has been searched

5 Linear Search bool LinSearch(double x[ ], int n, double item){ for(int i=0;i<n;i++){ if(x[i]==item) return true; else return false; } return false; }

6 Search Algorithms Suppose that there are n elements in the array. The following expression gives the average number of comparisons: It is known that Therefore, the following expression gives the average number of comparisons made by the Linear search in the successful case:

7 Search Algorithms

Linear Search Tradeoffs Linear Search is not very efficient. In the worst case, the target we are searching could be placed at the very end of the array, requiring that we search every single element in the array. On average, the target might be somewhere in the middle of the array, requiring that we search half of all the elements in the array. For example, if we had a 1000 element array: – worst case: we must search 1000 elements – on average: we search about 500 elements 8

Binary Search Binary Search is a more efficient option for searching arrays. There are two tricks to using Binary Search: – First, the array must be sorted. (Before you use Binary Search, you might first sort the array.) – Second, Binary Search works by dividing the search area in half after each comparison (more on this soon.) Binary Search is used very commonly in computer science, and there is a related concept called Binary Search Trees. – If you take an Algorithms course, you will definitely spend time researching Binary Search Trees. 9

10 Binary Search O(log2 n) A binary search looks for an item in a list using a divide-and- conquer strategy

11 Binary Search – Binary search algorithm assumes that the items in the array being searched are sorted – The algorithm begins at the middle of the array in a binary search – If the item for which we are searching is less than the item in the middle, we know that the item won’t be in the second half of the array – Once again we examine the “middle” element – The process continues with each comparison cutting in half the portion of the array where the item might be

12 Binary Search

13 Binary Search: middle element left + right 2 mid =

14 Binary Search bool BinSearch(double list[ ], int n, double item, int&index){ int left=0; int right=n-1; int mid; while(left<=right){ mid=(left+right)/2; if(item> list [mid]){ left=mid+1; } else if(item< list [mid]){right=mid-1;} else{ item= list [mid]; index=mid; return true; } }// while return false; }

15 Binary Search: Example

16 Binary Search

17 Binary Search

18 Binary Search Tree

19 Binary Search Tree O(log2 n) bool search(Node * root, int id) { bool found = false; if(root == NULL) return false; if(root->data == id) { cout<<" The node is found "<<endl; found = true; } if(!found) found = search(root->left, id); if(!found) found = search(root->right, id); return found; }

20 Binary Search Tree

21 Binary Search Tree

22 Binary Search Tree

Binary Search Efficiency Binary Search is very efficient. For example, if you have 1024 elements in you array, in the worst case, binary search only requires 10 comparisons. – Linear Search Worst Case: 1024 – Binary Search Worst Case: 10 If you have 1 billion elements, binary search only requires 30 comparisons! – Linear Search Worst Case: 1 billion – Binary Search Worst Case: 30! 23

Thank You 24