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

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

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

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

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

--anonymous

Page 2: 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

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

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

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

Binary Search

Figure 9.14 Trace of the binary search

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

Sequential vs Binary Search

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

MyMap Container with Binary Search

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

Hashing• Many types of hashing

▫String 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 Hashing Used for data/user verification and authentication.

▫Geometric Hashing This form of hashing is used in the field of

computer vision▫And more...

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

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]

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

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

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

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

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

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”

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

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”

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

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

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

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”

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

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”

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

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”

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

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”

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

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”

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

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.

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

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.

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

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.

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

MyMap as a Hash Table