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.

Slides:



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

CSE Lecture 3 – Algorithms I
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Chapter 19: Searching and Sorting Algorithms
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.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
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
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.
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.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
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.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching 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.
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.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
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.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
 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.
Chapter 16: Searching, Sorting, and the vector Type.
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 and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Introduction to Search Algorithms
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.
Searching and Sorting Arrays
Search,Sort,Recursion.
Data Structures and Algorithm: SEARCHING TECHNIQUES
Searching and Sorting Arrays
Presentation transcript:

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; 1. Sequential search. Sequential search on unsorted data. Sequential search on sorted data. 2. 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 IndexemployeeIDemployeeICempNamePost [0] Ahmad Faiz Azhar Programmer [1] Mohd. Azim Mohd. Razi Clerk [2] 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 2.

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 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: 1. Every element in the array will be examine sequentially, starting from the first element. 2. The process will be repeated until the last element of the array or until the searched data is found. 10 Basic Sequential Search

Used for searching that involves records stored in the main memory (RAM) The simplest search algorithm, but is also the slowest Searching strategy: 1. Examines each element in the array one by one (sequentially) and compares its value with the one being looked for – the search key 2. Search is successful if the search key matches with the value being compared in the array. Searching process is terminated. 3. 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 (BS) Search

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 12 Basic Sequential Search Function Every element in the array will be examined until the search key is found Or until the search process has reached the last element of the array

13 search_key [0][1][2][3][4] index array 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

14 p=0 p= [0][1][2][3][4] [0][1][2][3][4] index array search_key array 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 BS Search implementation – Search key = 22

15 p= [0][1][2][3][4] search_key index array 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 BS Search implementation – Search key = 22 Search for key 22 is successful & return 2

16 false [0][1][2][3][4] search_key found array 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 BS Search implementation – Search key = 25 p=0,1,2,3,4 => search key is not matches Search is unsuccessful index false [0][1][2][3][4] search_key found array index

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 Sequential Search Analysis

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 : 1. the searched key is found. 2. Or until the searched key value is smaller than the item compared in the list. => This will minimize the searching process. 18 Improvement of Basic Sequential Search Tech.

Sequential Searching on Sorted Data 19 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 break; } // end else-if }//end for return index; // return the value of index } //end function

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

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

Step 4 p = 3 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 [0][1][2][3][4] index array search_key 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 “ ” in the loop for 23 Steps to Execute Sequential Search Function on a Sorted List

3.0 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 ). 27 Binary Search 35 search_key

Binary Search Function 28 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

Binary Search on a Sorted List Search starts by obtaining the MIDDLE index of the array assume: search_key = 35 MIDDLE = ( ) / 2 = 3{ First MIDDLE index} search_key array

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 = ( ) / 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 Binary Search on Sorted List

Step 1 Steps to Execute Binary Search 32 false [0][1][2][3][4][5][6][7][8][9] LEFTMIDDLERIGHT LEFT MIDDLE RIGHT found index search_key array Assume: search_key = 22, array_size = 10

Step [0][1][2 ] [3][4][5][6][7 ] [8][9] MIDDLERIGHT false found index search_key array LEFT MIDDLE RIGHT Steps to Execute Binary Search

Step [0][1][2][3][4][5][6][7][8][9] LEFT MIDDLE RIGHT LEFT MIDDLE RIGHT true status index search_key array Steps to Execute Binary Search

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 ( ½ ) k n. 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 ( ½ ) k n. 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(log 2 n). 39 Binary Search Analysis

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 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 Search Conclusion