Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.

Similar presentations


Presentation on theme: "CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM."— Presentation transcript:

1 CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM

2 SEARCHING Searching : Retrieving the Information Stored in the computer is one of the most important applications of computer. Example of Applications Looking for a Name by giving the telephone number. Databases - student record, staff record, sales record Internet - search engine : Yahoo, Google 2

3 TYPES OF SEARCHING  Serial Search ◦ simplest, O(n)  Binary Search ◦ average-case O(log n)  Search by Hashing ◦ better average-case performance 3

4 SEQUENTIAL SEARCHING Start at the first piece of data and look at it and if it no the data you want then keep going until you find what you are looking for or until you have reached the last. 4

5 SEQUENTIAL SEARCH - ALGORITHM 1. Initialize a counter to zero 2. While the target is not found and the counter does not equal the last subscript in the array do steps 3 and 4. 3. Increment the counter 4. If array [counter] equals the target value then found becomes true. 5. When the loop ends return whether the target value was or was not found. 5

6 SEQUENTIAL SEARCH - ALGORITHM int LinearSearch (int array[], int target){ int i = 0, result = -1; bool found = 0; while( (i<n) && (found ==0) ) { //n is the array size if (array[i] == target){ result =i; //target position found = 1; //to break loop } i++; } return result; } 6

7 SEQUENTIAL SEARCH 7 Two Kinds of result 1) Successful 2) UnSuccessful Best Case - If the element to be searched is in the first location then the number of comparison is one. Worst Case - nth element O(n) -> (last) Average Case - 1/2 n (n+1)

8 SEQUENTIAL SEARCH Advantage : It is useful for limited sized data sets as it is simple and does not require data to be structured in any way Disadvantage : Time consuming (large list) 8

9 BINARY SEARCH How it works? Binary - Two. List is broken down to two parts and searched Working : Compare the target with the one in the center of the list and then restrict our attention to only the first or second half depending on whether the target comes before or after the central one. (we reduce the length to be searched by half) 9

10 BINARY SEARCH Rule - numbers must be in sorted order Steps: 1.Mark the first element as LOW and last element as HI 2.Find the mid point -> MID = (LOW + HI) /2 3.Check If MID = key, return index, where key is the target. If key > MID, LOW = MID +1 and carry on the same process If key > MID, HI = MID-1 and carry on the same process 4.Repeat the same process until you find the value (target) or if LOW and HI cross each other 10

11 BINARY SEARCH Algorithm low = 0; hi = n – 1; while (low <= hi ) mid = (low + hi) /2; if (key = = k(mid)) return(mid); if (key < k(mid)) hi = mid – 1; else low = mid + 1; end while return (-1); 11

12 BINARY SEARCH- EXAMPLE 1 12 462778178212236101491 LOW =0 HI=11 MID = (LOW + HI) /2 = 11/2 =5 Target : 62 MID = (6+11)/2 = 8 462778178212236101491 Compare Mid with Search Value - Yes it matches, So print out Element Found Compare MID with Search value - MID is Smaller than Search Value So move LOW to MID + 1 and proceed LOW HI=11

13 BINARY SEARCH- EXAMPLE 2 13 462778178 21 2236101491 LOW =0 HI=11 MID = (LOW + HI) /2 = 11/2 =5 Target : 9 MID = (0+4)/2 = 2 4 627781 78 212236 1014 91 Compare Mid with Search value - MID is Greater than Search Value So move HI to MID - 1 and proceed Compare MID with Search value - MID is Smaller than Search Value So move LOW to MID + 1 and proceed FIRST =0 LOW=MID - 1 = 5-1 = 4

14 14 MID = (3+4)/2 = 3 4 627781 78 212236 1014 91 Compare MID with Search value - MID is Greater than Search Value So move HI to MID - 1 and proceed MID = (3+3)/2 = 3 4 627781 78 212236 1014 91 Compare MID with Search value - MID is Greater than Search Value So move HI to MID - 1 and proceed Compare MID with Search value - LOW and HI cross each other, so target is Not Found in the list. MID = (3+3)/2 = 3 4 627781 78 212236 1014 91 BINARY SEARCH- EXAMPLE 2

15 EXERCISE Consider this : Using binary search method,  search 15  search 12 15 2791113153845

16 HASHING - INTRODUCTION Consider this : We have an array of all students in tiny college and the student id numbers from 1 to 200. Now if we want to look for any particular student we simply go to the location of their student id( KEY - value used for searching) and retrieve. If the students’ Id is 10 digit long then we need to convert that large number into a smaller number. 16

17 THE HASH FUNCTION The Keys may be integer, characters or some kind of data and we use the hash function to turn them into integers in the range of 0..m-1 where m is the size of the array of records. This function may or may not be simple. The term hash and hashing come from the very fact that the conversion from key to index really does ‘hash’ the key as in many cases the index resulting from hashing the key bears absolutely no resemblance to the original key at all. 17

18 HASHING Hashing is the ideal search, we would know exactly where the data are and go directly there. A search technique in which the required record/ key is located by using a function. 18 Key (k) Index/ reference Hash Function H (k) Example: Table size / array size is 10; therefore the index range is 0-9; if key is 29, H (key) = key % 10. So, H(29) = 29 % 10 = 9. Therefore, key 29 will store in array with index 9

19 THE HASH FUNCTIONS Two uses of hash function: ◦ Access the list element ◦ Location store the element There are several type of hash function: ◦ Truncation ◦ Modular Arithmetic (Division) ◦ Digit Extraction Method 19

20 HASHING METHOD - TRUNCATION Exercise: ◦ Write the hash function to truncate first 3 digits of a given 5 digits number.  E.g. key = 26785; Hash (index) = 85 ; Hash (key) = ?? ◦ Write the hash function to truncate last 3 digits of a given 5 digits number.  E.g. key = 26785; Hash (index) = 26; Hash (key) =?? ◦ Write the hash function to truncate first digits and last 2 digits of a given 5 digits number.  E.g. key = 26785; Hash (index) = 67; Hash (key) =?? 20 key %100 key /1000 key /100%100 or key %10000/100

21 HASHING METHOD - MODULAR ARITHMETIC The key is converted into an integer, the integer is then divided by the size of the index range and the remainder is taken as the index position of the record. index = key % size As an example, suppose we have an 5 digit integer as a key and that there are 1000 records (and room for 1000 records). The hash function would then be: index = KEY % size e.g., 12345 % 1000 If we are very lucky! our keys might be such that there is only 1 key that maps to each index. Of course we might still have the situation where two keys map to the same index (e.g. 23456, 43456) - this is called a COLLISION. 21

22 WHAT IS COLLISION SAME INDEX TWO If the hashing Function gives out SAME INDEX for TWO keys then it is called a collision e.g. : 123 % 10 index will be 3 223 % 10 index will be again 3 We cannot store two values in the same location 22

23 HOW TO RESOLVE COLLISIONS? Two methods are discussed here: 1.Resolving collisions by OPEN ADDRESSING Linear probing Quadratic probing 2.Resolving collisions by CHAINING 23

24 RESOLVING COLLISIONS BY OPEN ADDRESSING: Linear Probing : ◦ Start at the point where the collision occurred and do a sequential search through the table for an empty location. Improvement : ◦ Circular Probing : After reaching the end start probing from the first 24

25 EXAMPLE - LINEAR PROBING The records with the keys 456, 235, 268, 428, 104, 273, 126, 316 are to be stored in an array of size 10 using the hash function.Hash function = (key) mod 10 Solution: 25 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Keys456235268428104273126316 Index 6 5 884366 456 235 268 428104273126316

26 RESOLVING COLLISIONS BY OPEN ADDRESSING: Quadratic Probing: If there is a collision at the address ‘h’, this method probes the table at locations ( Hash value + i 2 ) mod (table_ size) with i=1,2,3……….. 26

27 Resolving collisions by Open addressing: Example: Insert the following data into a hash table using quadratic probing as the collision resolution strategy. Assume tableSize = 10, h(key)=key % 10. The keys are 17 26 38 9 7 27 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Hash (17) = 17%10 = 7 17 Hash (26) = 26 %10 = 6 26 Hash (38) = 38 %10 = 8 38 Hash (9) = 9 %10 = 9 9 Hash (7) = 7 %10 = 7 f(i) = i 2 ; f(1) = 1 2 = 1 h 1 (7) =(hash (7) + 1) %10 = (7 + 1) %10 = 8 f(i) = i 2 ; f(2) = 2 2 = 4 h 2 (7) =(hash (7) + 4) %10 = (7 + 4) %10 = 1 7

28 RESOLVING COLLISIONS BY CHAINING Implemented using Linked List Whenever a collision occurs a new node is created and the new value is stored and linked to the old value. Key17263897 Index76897 28 0 1 2 3 4 5 6 7 8 9 17 26 38 9 7

29 The End 29


Download ppt "CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM."

Similar presentations


Ads by Google