Data Structures and Algorithm: SEARCHING TECHNIQUES

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

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.
 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.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Searching and Sorting Arrays
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 8 ARRAYS Continued
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.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Problem Solving and Algorithms
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Applications of Arrays (Searching and Sorting) and Strings
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.
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.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
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.
Data Structure Introduction.
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):
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
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.
ALGORITHMS.
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.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Chapter 16: Searching, Sorting, and the vector Type.
Searching Arrays Linear search Binary search small arrays
Recursion Powerful Tool
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Introduction to Search Algorithms
Chapter 15 Recursion.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Recitation 13 Searching and Sorting.
Data Structures Interview / VIVA Questions and Answers
Siti Nurbaya Ismail Senior Lecturer
Linear and Binary Search
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Introduction to Search Algorithms
Searching and Sorting Arrays
Algorithm Efficiency and Sorting
Lecture Set 9 Arrays, Collections and Repetition
Search,Sort,Recursion.
Linear Search Binary Search Tree
24 Searching and Sorting.
Binary Search A binary search algorithm finds the position of a specified value within a sorted array. Binary search is a technique for searching an ordered.
Topic 24 sorting and searching arrays
Searching and Sorting Arrays
Search,Sort,Recursion.
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

Data Structures and Algorithm: SEARCHING TECHNIQUES

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 searching operation. Understand the implementation of basic searching algorithm; Sequential search. Sequential search on unsorted data. Sequential search on sorted data. Binary Search. Able to analyze the efficiency of the searching technique. Able to implement searching technique in problem solving.

1.0 Introduction

Class Content

Introduction Searching Definition Clifford A. Shaffer[1997] define searching as a process to determine whether an element is a member of a certain data set. The process of finding the location of an element with a specific value (key) within a collection of elements The process can also be seen as an attempt to search for a certain record in a file. Each record contains data field and key field Key field is a group of characters or numbers used as an identifier for each record Searching can done based on the key field.

Example: Table of Employee Record Index employeeID employeeIC empName Post [0] 1111 701111-11-1234 Ahmad Faiz Azhar Programmer [1] 122 800202-02-2323 Mohd. Azim Mohd. Razi Clerk [2] 211 811003-03-3134 Nurina Raidah Abdul Aziz System Analyst Searching can be done based on certain field: empID, or empl_IC, or empName To search empID = 122, give us the record value at index 1. To search empID = 211, give us the record value at index 1.

Introduction Among Popular searching techniques: Sequential search Binary Search Binary Tree Search Indexing Similar with sorting, Searching can also be implemented in two cases, internal and external search.

Introduction Similar with sorting, Searching can also be implemented in two cases, internal and external search. External search – only implemented if searching is done on a very large size of data. Half of the data need to be processed in RAM while half of the data is in the secondary storage. Internal search – searching technique that is implemented on a small size of data. All data can be load into RAM while the searching process is conducted. The data stored in an array

2.0 Basic Sequential Search

Basic Sequential Search Basic sequential search usually is implemented to search item from unsorted list/ array. The technique can be implemented on a small size of list. This is because the efficiency of sequential search is low compared to other searching techniques. In a sequential search: Every element in the array will be examine sequentially, starting from the first element. The process will be repeated until the last element of the array or until the searched data is found. 10

Basic Sequential (BS) Search Used for searching that involves records stored in the main memory (RAM) The simplest search algorithm, but is also the slowest Searching strategy: Examines each element in the array one by one (sequentially) and compares its value with the one being looked for – the search key Search is successful if the search key matches with the value being compared in the array. Searching process is terminated. else, if no matches is found, the search process is continued to the last element of the array. Search is failed array if there is no matches found from the array. 11

Basic Sequential Search Function int SequenceSearch( int search_key, const int array [ ], int array_size ) { int p; int index =-1; //-1 means record is not found for ( p = 0; p < array_size; p++ ){ if ( search_key == array[p] ){ indeks = p;//assign current array index break; }//end if } //end for return index; } //end function Or until the search process has reached the last element of the array Every element in the array will be examined until the search key is found 12

BS Search implementation – Search key = 22 int SequenceSearch( int search_key, const int array [ ], int array_size ) { int p; int index =-1; //-1 means record is not found for ( p = 0; p < array_size; p++ ){ if ( search_key == array[p] ){ indeks = p; //assign current array index break; }//end if } //end for return index; } //end function -1 index [0] [1] [2] [3] [4] array 11 33 22 55 44 search_key 22 13

BS Search implementation – Search key = 22 int SequenceSearch( int search_key, const int array [ ], int array_size ) { int p; int index =-1; //-1 means record is not found for ( p = 0; p < array_size; p++ ){ if ( search_key == array[p] ){ indeks = p; //assign current array index break; }//end if } //end for return index; } //end function index -1 [0] [1] [2] [3] [4] array 11 33 22 55 44 search_key 22 index -1 [0] [1] [2] [3] [4] array 11 33 22 55 44 search_key 22 14

BS Search implementation – Search key = 22 int SequenceSearch( int search_key, const int array [ ], int array_size ) { int p; int index =-1; //-1 means record is not found for ( p = 0; p < array_size; p++ ){ if ( search_key == array[p] ){ indeks = p; //assign current array index break; }//end if } //end for return index; } //end function p=2 index 2 [0] [1] [2] [3] [4] array 11 33 22 55 44 search_key 22 Search for key 22 is successful & return 2 15

BS Search implementation – Search key = 25 int SequenceSearch( int search_key, const int array [ ], int array_size ) { int p; int index =-1; //-1 means record is not found for ( p = 0; p < array_size; p++ ){ if ( search_key == array[p] ){ indeks = p; //assign current array index break; }//end if } //end for return index; } //end function found false index -1 [0] [1] [2] [3] [4] array 11 33 22 55 44 search_key 25 found false index -1 [0] [1] [2] [3] [4] array 11 33 22 55 44 p=0,1,2,3,4 => search key is not matches Search is unsuccessful search_key 25 16

Sequential Search Analysis Searching time for sequential search is O(n). If the searched key is located at the end of the list or the key is not found, then the loop will be repeated based on the number of element in the list, O(n). If the list can be found at index 0, then searching time is, O(1). 17

Improvement of Basic Sequential Search Tech. Problem: Search key is compared with all elements in the list, O(n) time consuming for large datasets. Solution: The efficiency of basic search technique can be improved by searching on a sorted list. For searching on ascending list, the search key will be compared one by one until : the searched key is found. Or until the searched key value is smaller than the item compared in the list. => This will minimize the searching process. 18

Sequential Searching on Sorted Data int SortedSeqSearch ( int search_key, const int array[ ], int array_size) { int p; int index = -1; //-1 means record not found for ( p = 0; p < array_size; p++ ) { if (search_key < array [p] ) break; // loop repetition terminated // when the value of search key is // smaller than the current array element else if (search_key == array[p]) { index = p; // assign current array index } // end else-if }//end for return index; // return the value of index } //end function 19

Steps to Execute Sequential Search Function on a Sorted List Assume: search_key = 25 array_size = 5 Step 1 Initial value for variable index and array elements index -1 [0] [1] [2] [3] [4] array 11 22 33 44 55 search_key 25 20

Steps to Execute Sequential Search Function on a Sorted List search_key is compared with the first element in the array Step 3 p = 2 search_key is compared with the second element in the array index -1 [0] [1] [2] [3] [4] array 11 22 33 44 55 search_key 25 index -1 [0] [1] [2] [3] [4] array 11 22 33 44 55 search_key 25 21

Steps to Execute Sequential Search Function on a Sorted List search_key is compared with the third element in the array • the value of search_key is smaller than the current element of array • loop repetition is terminated using “break” statement index -1 [0] [1] [2] [3] [4] array 11 22 33 44 55 search_key 25

Steps to Execute Sequential Search Function on a Sorted List Conclusion: If the elements in the list is not in a sorted (asc/desc) order, loop will be repeated based on the number of elements in the list When the list is not sorted the loop is repeated 5 times, compared to 3 times if the list is in sorted order as shown in the previous example. If the list is sorted in descending order, change operator “<“ to operator “>” in the loop for 23

3.0 Binary Search

Binary Search The drawback of sequential search algorithm is having to traverse the entire list, O(n) Sorting the list does minimize the cost of traversing the whole data set, but we can improve the searching efficiency by using the Binary Search algorithm 25

Binary Search Consider a list in ascending sorted order. For a sequential search, searching is from the beginning until an item is found or the end is reached. Binary search improve the algorithm by removing as much of the data set as possible so that the item is found more quickly. Search process is started at the middle of the list, then the algorithm determine which half the item is in (because the list is sorted). It divides the working range in half with a single test. By repeating the procedure, the result is an efficient search algorithm-O(log2 n). 26

Binary Search starts by comparing the search key with the element at the middle If the value matches, it will be return to the calling function (index = MIDDLE) If the search key < the middle element, search will be focused on the elements between the first element to the element before the middle element (MIDDLE -1) If the search key is not found, the element at the middle of the first element and the MIDDLE –1 element will be compared with the search key. If the search key > the middle element, search will only be focused on the elements between the second MIDDLE element to the first MIDDLE element. Search is repeated until the searched key is found or the last element in the subset is traversed (LEFT>RIGHT ). search_key 35 27

Binary Search Function int binary_search( int search_key, int array_size, const int array [] ) { bool found = false; int index = -1 //-1 means record not found int MIDDLE, LEFT = 0, RIGHT = array_size-1; while (( LEFT<= RIGHT ) && (!found)) { MIDDLE = (LEFT + RIGHT )/ 2; // Get middle index if ( array[MIDDLE] == search_key) { index = MIDDLE; found = true; } else if (array[MIDDLE] > search_key) RIGHT= MIDDLE– 1; // search is focused // on the left side of list else LEFT= MIDDLE+ 1 // search is focused // on the right side of the list } //end while return index; }//end function 28

Binary Search on a Sorted List search_key 35 array 11 22 33 44 55 66 77 Search starts by obtaining the MIDDLE index of the array assume: search_key = 35 MIDDLE= ( 0 + 6 ) / 2 = 3 { First MIDDLE index} 29

Binary Search on Sorted List search_key 35 is compared with the element at the fourth index in the array, which is array[3] with the value 44. search_key < MIDDLE value, therefore search will be focused on the elements between the first index and the third index only (index 1 to MIDDLE-1) Process to obtain MIDDLE index is repeated MIDDLE = ( 0 + 2 ) / 2 = 1 { second MIDDLE index} Search_key 35 is compared with the element at the second index, which is array[1] with the value 22 search_key > MIDDLE value, therefore search will be focused only on the elements between the second MIDDLE index to the first MIDDLE index. MIDDLE = (2 + 2 ) / 2 = 2 { third MIDDLE index} 30

Binary Search on Sorted List Element at the third index, array[2] with the value 33 is not equal to the value of the search key. Search process has reached the last element of the traversed subset, therefore search is terminated and assumed fail. To search from the list sorted descending, change operator “ > ” to operator “ < “ to the following statement : else if (array [ MIDDLE ] > search_key) RIGHT = MIDDLE – 1; 31

Steps to Execute Binary Search found false index -1 search_key 22 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] array 12 14 16 18 20 22 24 26 28 30 LEFT MIDDLE RIGHT LEFT RIGHT 9 MIDDLE 4 Assume: search_key = 22, array_size = 10 32

Steps to Execute Binary Search found false index -1 search_key 22 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] array 12 14 16 18 20 22 24 26 28 30 LEFT MIDDLE RIGHT LEFT 5 9 RIGHT MIDDLE 7 33

Steps to Execute Binary Search status true index 5 search_key 22 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] array 12 14 16 18 20 22 24 26 28 30 LEFT RIGHT MIDDLE LEFT 5 RIGHT 6 MIDDLE 5 34

Binary Search Analysis Binary Search starts searching by comparing element in the middle. Thus the searching process start at n/2 for a list size = n. If the middle value does not matches with the search key, then the searching area will be reduced to the left or right sublist only. This will reduce the searching area to ½ n. From half of the list, the second middle value will be identified. Again, if the middle value does not matches with the search key, the searching area will be reduced to the left or right sub list only. The searching area will reduce ½ ( ½ n). 35

Binary Search Analysis The process of looking for middle point and reducing the searching area to the left or right sublist will be repeated until the middle value is equal to the middle value (search key is found) or the last value in sublist has been traverse. If the repetition occur k times, then at iteration k , the searching area is reduced to ( ½ )kn. 36

Binary Search Analysis figure above shows the reducing size for binary search area. At iteration k for array size = n , searching area will be reduced from n to ( ½ )kn. 37

Binary Search Analysis Searching from 1 billion data using Binary search 38

Binary Search Analysis Best case for binary search happen if the search key is found in the middle array. O(1). Worse case for binary search happen if the search key is not in the list or the searching size equal to 1. The searching time for worse case is O(log2n). 39

Basic searching techniques : sequential search and binary search. Searching is a process to allocate an element in a list and return the index of the searched element. Basic searching techniques : sequential search and binary search. Sequential search can be implemented on sorted and unsorted list, while binary search can be implemented Search Conclusion only on sorted list. Sequential search on sorted data is more efficient than sequential search on unsorted data. Binary search is more efficient than sequential search. Basic searching techniques explained in this class are only suitable for small sets of data. Hashing and indexing are suitable for searching large sets of data. 40