



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of data structure and algorithm concepts, specifically focusing on searching techniques such as linear search and binary search, and hash functions. The process of searching for an element in a collection, the advantages of different searching techniques, and the concept of hash functions for indexing records. It also discusses hash collisions and methods for handling them, including open addressing and chaining.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Nepal College of Information Technology Chapter 8: Searching Searching is a process of checking and finding an element from a list of elements. Let A be a collection of data elements, i.e., A is a linear array of say n elements. If we want to find the presence of an element “data” in A, then we have to search for it. The search is successful if data does appear in A and unsuccessful if otherwise. There are several types of searching techniques; one has some advantage(s) over other. Following are the three important searching techniques:
Nepal College of Information Technology Then apply the following conditions to search a “data”.
Nepal College of Information Technology Hash Table So if you enter the employee code to the hash function, we can directly retrieve TABLE[H( k )] details directly. Note that if the memory address begins with 01- m instead of 00- m , then we have to choose the hash function H( k ) = k (mod m )+1. 3.2. Mid Square Method The key k is squared. Then the hash function H is defined by H(k) = k^2 = l Where l is obtained by digits from both the end of k 2 starting from left. Same number of digits must be used for all of the keys. For example consider following keys in the table and its hash index : Hash Table with Mid Square Division 3.3. Folding Method The key K, K 1 , K 2 ,...... Kr is partitioned into number of parts. The parts have same number of digits as the required hash address, except possibly for the last part. Then the parts are added together, ignoring the last carry. That is H(K) = K 1 + K 2 + ...... + Kr Here we are dealing with a hash table with index form 00 to 99, i.e , two-digit hash table. So we divide the K numbers of two digits. Extra milling can also be applied to even numbered parts, K 2 , K 4 , ...... are each reversed before the addition(second table in above). H(7148) = 71 + 64 = 155, here we will eliminate the leading carry (i.e., 1). So H(7148) = 71 + 64 = 55.
Nepal College of Information Technology 3.4. Hash Collision and Handling of Hash Collision It is possible that two non-identical keys K 1 , K 2 are hashed into the same hash address. This situation is called Hash Collision. Let us consider a hash table having 10 locations as in table. Division Method is used to hash the key. H(K) = K (mod) m ; Here m is chosen as 10. The Hash function produces any integer between 0 and 9 inclusions, depending on the value of the key. If we want to insert a new record with key 500 then H(500) = 500(mod 10) = 0. The location 0 in the table is already filled (i.e., not empty). Thus collision occurred. Collisions are almost impossible to avoid but it can be minimized considerably by introducing any one of the following three techniques: Table