Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching “It is better to search, than to be searched” --anonymous.

Similar presentations


Presentation on theme: "Searching “It is better to search, than to be searched” --anonymous."— Presentation transcript:

1 Searching “It is better to search, than to be searched” --anonymous

2 Sequential Search Search begins at the beginning of the container and continues until the item is found or the entire list has been searched

3 Binary Search Search begins at the middle and finds the item ▫Or it eliminates half of the unexamined items  Search begins at the middle and finds the item  Or it eliminates half of the unexamined items ▫Search begins at the middle and finds the item ▫Or it eliminates half of the unexamined items ▫Search begins at the middle and finds the item ◦Or it eliminates half of the unexamined items ◦Repeat these steps until the item is found

4 Binary Search Figure 9.14 Trace of the binary search

5 Sequential vs Binary Search

6 MyMap Container with Binary Search

7 Hashing Many types of hashing ▫String HashingString Hashing  Used in the area of data storage access. Mainly within indexing of data and as a structural back end to associative containers(ie: hash tables) ▫Cryptographic HashingCryptographic Hashing  Used for data/user verification and authentication. ▫Geometric HashingGeometric Hashing  This form of hashing is used in the field of computer vision ▫And more...

8 What is a Hash Table ? The simplest kind of hash table is an array of records. This example has 701 records. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] An array of records... [ 700]

9 What is a Hash Table ? Each record has a special field, called its key. In this example, the key is a long integer field called Number. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ]... [ 700] [ 4 ] Number 506643548

10 What is a Hash Table ? The number might be a person's identification number, and the rest of the record has information about the person. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ]... [ 700] [ 4 ] Number 506643548

11 What is a Hash Table ? When a hash table is in use, some spots contain valid records, and other spots are “empty” [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] An array of records... [ 700] 28474635 “Joe” 37814026 “Sam” 74856204 “Ann”

12 Inserting a New Record ? In order to insert a new record, the key must somehow be converted to an array index The index is called the hash value of the key [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] An array of records... [ 700] 28474635 “Joe” 37814026 “Sam” 74856204 “Ann”

13 Inserting a New Record ? In order to insert a new record, the key must somehow be converted to an array index The index is called the hash value of the key Simple hash: find (key mod array_size) ▫What is ( 580625685 mod 701)? [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] An array of records... [ 700] 28474635 “Joe” 37814026 “Sam” 74856204 “Ann” 580625685 “Sarah” new

14 What is a Hash Table ? The hash value is used for the location of the new record [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ]... [ 700] [ 3] Number 506643548 “Sarah” 28474635 “Joe” 37814026 “Sam” 74856204 “Ann”

15 Collisions Here is another new record to insert, with a hash value of 2. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] An array of records... [ 700] 28474635 “Joe” 37814026 “Sam” 74856204 “Ann” 701466868 “Tim” new 580625685 “Sarah”

16 Collisions Move forward until you find an empty spot [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] An array of records... [ 700] 28474635 “Joe” 37814026 “Sam” 74856204 “Ann” 701466868 “Tim” new 580625685 “Sarah”

17 Collisions Move forward until you find an empty spot [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] An array of records... [ 700] 28474635 “Joe” 37814026 “Sam” 74856204 “Ann” 701466868 “Tim” new 580625685 “Sarah”

18 Collisions The new record is placed in the first available spot after the hash value [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] An array of records... [ 700] 28474635 “Joe” 37814026 “Sam” 74856204 “Ann” 701466868 “Tim” new 580625685 “Sarah”

19 Searching for a key Calculate the hash value Check that location of the array for the key Keep moving forward until you find the key or you reach an empty spot.

20 Deleting a Record Records may be deleted from a hash table But the location must not be left an an ordinary “empty spot” because that could interfere with searches The location must be marked as deleted so the search can tell that it used to have something in it.

21 Chained Hashing Each data[i] in the array is a pointer to a linked list When a collision occurs, the new data element is “chained” into the linked list for that index number.

22 MyMap as a Hash Table


Download ppt "Searching “It is better to search, than to be searched” --anonymous."

Similar presentations


Ads by Google