AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.: 0511363.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Recursion.
CSE Lecture 3 – Algorithms I
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
Binary Search Visualization i j.
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 Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Data Structures Using Java1 Chapter 8 Search Algorithms.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Sequential Search slides. Searching Searching : –Information retrieval is one of the most important application of computers. –EG: Looking for a Name.
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.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
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
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Data Structures Using Java1 Chapter 8 Search Algorithms.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Chapter 19: Searching and Sorting Algorithms
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.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
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.
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Data Structures & Algorithms Week 4. CHAPTER 3: SEARCHING TECHNIQUES 1. LINEAR (SEQUENTIAL) SEARCH 2.BINARY SEARCH 3. COMPLEXITY OF ALGORITHMS.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
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.
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
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.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
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
Search Algorithms Sequential Search (Linear Search) Binary Search
Searching: linear & binary
Data Structures: Searching
Applications of Arrays
Presentation transcript:

AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.: Fall 2014

What is Searching Algorithm? Searching techniques enables you to find location of data item in a specified array of data items. by matching each data item with the desired data item one by one as in the sequential search, or matching each data item randomly in binary search.

Sequential Search Sequential Search (linear search): The search always starts at the first element in the list and continues until either the item is found in the list or the entire list is searched. Sequential search on linked lists and on array was covered in previous chapters

Sequential Search #include int sequential(int x[ ],int n,int key) { for(int i=0;i<n;i++) if(key==x[i]) return i; return -1; } main() { int m; int x[7]={9,7,8,2,4,3,6}; cin>>m; cout<<sequential(x,8,m)<<endl; }

Time complexity The complexity is dependent on the size of the list. The worst case for the search is if the key is not found. At that point the whole list has been traversed so if the list has n elements, the algorithm is O(n). The best case is also simple. The key is found in the first element so it is O(1). The average case is n/2. What this says is that on the average the key will be found in the first half of the list --not an unreasonable expectation.

Binary Search A binary search can be performed only on ordered lists The binary search algorithm uses the divide-and- conquer technique to search the list. First, the search item is compared with the middle element of the list. If the search item is found, the search terminates. If the search item is less than the middle element of the list, we restrict the search to the first half of the list; otherwise, we search the second half of the list. This is a powerful method.

Suppose that we want to determine whether 75 is in this list First, we compare 75 with the middle element in this list, list[5] (which is 39). Because 75 >list[5], we restrict our search to this list Example

#include int Binary_(int x[],int n,int key) { int Up=n-1, Lo=0, mid; while(Lo <= Up){ mid=(Lo+Up)/2; if(key==x[mid]) return mid; if(key<x[mid]) Up=mid-1; else Lo=mid+1; } return -1; } Binary Search Method main(){ int m; int x[7]={1,2,3,4,5,6,7}; cin>>m; cout<<Bi_(x,7,m)<<endl; }

Time complexity Given an array of 1023 elements, we can narrow the search to 511 items in one comparison. Another comparison, and we’re looking at only 255 elements. In fact, we can search the entire array in only 10 comparisons Thus the binary search is a O(lg n) algorithm, where n is the length of the list

Binary Search by Recursion #include int Binary(int x[], int L, int R, int Key){ if(L > R) return-1; else { int mid, index ; mid = (L + R) / 2; if( Key == x[mid] ) index=mid; else if( Key < x[mid]) index=Bi_(x, L, mid-1, Key) ; else index = Bi_(x, mid + 1, R, Key); return index; } main() { int m ; const int size = 6 ; int x[size]={ 10, 17, 45, 49, 55, 68} ; cin>>m; cout<<" The position of Key "<<m<<" is at position : " <<Bi_(x, 0, size-1, m)<<endl ; }