Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
CSE Lecture 3 – Algorithms I
HST 952 Computing for Biomedical Scientists Lecture 9.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Chapter 19: Searching and Sorting Algorithms
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
 1 Sorting. For computer, sorting is the process of ordering data. [ ]  [ ] [ “Tom”, “Michael”, “Betty” ]  [ “Betty”, “Michael”,
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
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 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Computer Programming Sorting and Sorting Algorithms 1.
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.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 16: Searching, Sorting, and the vector Type.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Data Strcutures.
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. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Data Structures and Algorithms Introduction to Algorithms M. B. Fayek CUFE 2006.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Data Structure Introduction.
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.
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.
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.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
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 Topics Sequential Search Binary Search.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Searching CS 110: Data Structures and Algorithms First Semester,
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 16: Searching, Sorting, and the vector Type.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
LINKED LISTS.
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 Searching algorithms with simple arrays
Data Structures I (CPCS-204)
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Linear and Binary Search
CSc 110, Spring 2017 Lecture 39: searching.
Searching CLRS, Sections 9.1 – 9.3.
Data Structures: Searching
Presentation transcript:

Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006

Agenda 1. Introduction 2. Sequential Search 3. Binary Search 4. Interpolation Search 5. Indexed Search

1. Introduction What is a Search? What is a Search? “ Searching is the task of finding a certain data item (record) in a large collection of such items.”  A key field that identifies the item sought for is given. (For simplification we consider only the key field instead of the complete record.)  If the item is found either its location or the complete item is returned.  If the item is not found an indication is given, usually by returning a non-existing index such as -1.

Agenda 1. Introduction 2. Sequential Search 3. Binary Search 4. Interpolation Search 5. Indexed Search

2. Sequential Search Sequential Search is also called Exhaustive Search because the complete collection is searched. Sequential Search is also called Exhaustive Search because the complete collection is searched.

2. Sequential Search NO YES Key item = list[i] ? Keyitem = 20 i =0 i =1 i =2 return i =2 as found location !

2. Sequential Search The first implementation will be: The first implementation will be: for i = 0 to n do get next item Ai if Ai == k return i endfor return -1

2. Sequential Search Another pseudo code is: Another pseudo code is: i =0 while i k i <-- i+1 if i < n return i else return -1 return -1 Check boundary conditions! ←

2. Sequential Search How is the algorithm implemented? How is the algorithm implemented? The way the collection is constructed affects the way the next item Ai is retrieved. The way the collection is constructed affects the way the next item Ai is retrieved. In a static array: Ai is the indexed item A[i] In a static array: Ai is the indexed item A[i] In a linked list: Ai is the next node to be fetched by following the “next pointer” in the present node. In this case usually the address of the node found (a pointer to the found node) is returned or a NULL pointer to indicate that it was not found In a linked list: Ai is the next node to be fetched by following the “next pointer” in the present node. In this case usually the address of the node found (a pointer to the found node) is returned or a NULL pointer to indicate that it was not found In a file: Ai is the next record retrieved from the file In a file: Ai is the next record retrieved from the file

2. Sequential Search Complexity: Complexity: The basic operation is the comparison The basic operation is the comparison For a collection of n data items there are several cases: For a collection of n data items there are several cases: Best case: item found at the first location Best case: item found at the first location  Number of comparisons = 1 Worst Case: item found at the last location or item not found Worst Case: item found at the last location or item not found  Number of comparisons = n Average case = (1+n)/2 Average case = (1+n)/2

2. Sequential Search Enhancements Sequential Search may be enhanced using several techniques: Sequential Search may be enhanced using several techniques: 1. Sorting before searching (Presorting) 2. Sentinel Search 3. Probabilistic Search

2. Sequential Search Enhancements 1. Presorting A good question to ask before searching is whether the collection is sorted or not? A good question to ask before searching is whether the collection is sorted or not? How do we use that info? If sorted the search is terminated as soon as the value of the indexed item in the collection exceeds that of the search item. How do we use that info? If sorted the search is terminated as soon as the value of the indexed item in the collection exceeds that of the search item. What is the effect? This will not affect the worst case of finding the element at the last position, but it will decrease the average number of comparisons if logic position of the item were somewhere before the end of the list and the element was not found. What is the effect? This will not affect the worst case of finding the element at the last position, but it will decrease the average number of comparisons if logic position of the item were somewhere before the end of the list and the element was not found. A more efficient search is the binary search. A more efficient search is the binary search.

2. Sequential Search Enhancements 2. Sentinel Search The basic loop in sequential sort include 2 comparisons at each iteration The basic loop in sequential sort include 2 comparisons at each iteration while( (i A [ i ]) ) To decrease the number of comparisons to one per iteration a sentinel value = key is inserted at the end of the array (beyond its end, i.e. at n) To decrease the number of comparisons to one per iteration a sentinel value = key is inserted at the end of the array (beyond its end, i.e. at n) Hence the first comparison is redundant. The search will always stop finding key either within A (if it already existed) or outside A if it originally did not exist. Hence the first comparison is redundant. The search will always stop finding key either within A (if it already existed) or outside A if it originally did not exist. A check on the location of key will indicate if it existed or not. A check on the location of key will indicate if it existed or not.

2. Sequential Search Enhancements 3. Probabilistic Search The basic idea here is that popular elements of the list that are searched for more frequently should require less comparisons to find The basic idea here is that popular elements of the list that are searched for more frequently should require less comparisons to find This is implemented by enhancing the location of an element found in the array when searched for, one location ahead by swapping it with the element before it. This is implemented by enhancing the location of an element found in the array when searched for, one location ahead by swapping it with the element before it. Hence, each time an element is found the number of comparisons needed to find it next time is decremented by one Hence, each time an element is found the number of comparisons needed to find it next time is decremented by one

2. Sequential Search Modifying the first sequential algorithm for the case of sorted list would be : Modifying the first sequential algorithm for the case of sorted list would be : for i = 0 to n do if Ai > k return -1 // as list is sorted the // possible location has been passed // possible location has been passed if Ai == k return i return -1

2. Sequential Search Modifying the second sequential algorithm for the case of sorted list would be : Modifying the second sequential algorithm for the case of sorted list would be : i =0 while i < n and next item Ai < k i <-- i+1 if Ai == k and i < n return i else return -1

Agenda 1. Introduction 2. Sequential Search 3. Binary Search 4. Interpolation Search 5. Indexed Search

3. Binary Search How does it work? How does it work?  Basic idea that dividing the list at each search step into 2 sublists and checking the mid item the range to be searched for possible location is either the left or right sublist (i.e. desreased to half ). Note however, that the determination of the middle item in the collection is a simple task if the data collection is represented in memory by a sequential array, whereas it is not so if the collection is represented using a linked list. Hence we will assume that the collection is a sequential array. Note however, that the determination of the middle item in the collection is a simple task if the data collection is represented in memory by a sequential array, whereas it is not so if the collection is represented using a linked list. Hence we will assume that the collection is a sequential array.

2. Sequential Search NO YES Key item = list[mid] ? Keyitem = 20 n = 8mid =4 return i =2 as found location ! Key item < list[mid] mid =2 3 comparisons! mid =3 Key item > list[mid]

3. Binary Search For the same input and output specs as before For the same input and output specs as before the algorithm is: low = 0; high = n-1; while (low < high) do { mid = (low+high)/2 if ( k < A [mid] ) then high = mid -1 if ( k < A [mid] ) then high = mid -1 else if ( k > A [mid] then low = mid +1 else if ( k > A [mid] then low = mid +1 else return mid // found else return mid // found } return -1 // not found

3. Binary Search Complexity: Complexity: For a collection of n data items: For a collection of n data items: In each step: the mid item is compared to k and the range of search is divided by 2 In each step: the mid item is compared to k and the range of search is divided by 2 This is repeated until the range is zero (at the worst case). This is repeated until the range is zero (at the worst case). i.e. we should ask: how many times will we divide n by 2 till the length of sublists is zero? i.e. we should ask: how many times will we divide n by 2 till the length of sublists is zero? → log 2 n … which is better than n → log 2 n … which is better than n

Agenda 1. Introduction 2. Sequential Search 3. Binary Search 4. Interpolation Search 5. Indexed Search

4.Interpolation Search What is meant by interpolation? What is meant by interpolation? Here we try to guess more precisely where the search key resides. Here we try to guess more precisely where the search key resides. Instead of calculating the middle as the physical middle (low+high)/2 it is calculated in a weighted manner w.r.t. to the value of k relative to max and min values in the list Instead of calculating the middle as the physical middle (low+high)/2 it is calculated in a weighted manner w.r.t. to the value of k relative to max and min values in the list

4. Interpolation Search Analysis: Analysis:  Calculations are more complex for mid  Significant Improvement in search time especially when values of data items in collection are evenly distributed.

Agenda 1. Introduction 2. Sequential Search 3. Binary Search 4. Interpolation Search 5. Indexed Search

What is an index? What is an index? Similar to the index of a book (e.g. telephone book), items in the index point to significant items in the collection. Similar to the index of a book (e.g. telephone book), items in the index point to significant items in the collection. This implies that in this search an additional table is used … the index table, where each item in the index table points to a specific location in the original search list. This implies that in this search an additional table is used … the index table, where each item in the index table points to a specific location in the original search list.

5. Indexed Search Algorithm: Algorithm: // Input: Search array A of n items + index table of d items + key item k //Output: Location of item with search key or false key Step 1: Determine search range for key within index table by specifying (i min to i max ) inside original search list Step 2: Search sequentially for key in range (i min to i max ) inside original search list

5. Indexed Search Algorithm: Algorithm: Index Table Searching for key =53 { Pos Step 1 Step 2 Pos = 5+1= 6 1

5. Indexed Search Analysis: Assuming that: Analysis: Assuming that:  the original table is of size n  Index is of size d Step 1: Determine search range has average complexity: O( d/2) Step 2: Search for key in range (i min to i max ) inside original search list, assume average range length = n/k