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

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

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

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

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

Binary Search Figure 9.14 Trace of the binary search

Sequential vs Binary Search

MyMap Container with Binary Search

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...

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]

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

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

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”

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”

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

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”

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”

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”

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”

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”

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.

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.

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.

MyMap as a Hash Table

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

Similar presentations