1 / 22

Searching

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

Télécharger la présentation

Searching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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 ] [ 700] . . . An array of records

  9. [ 4 ] Number 506643548 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] . . .

  10. [ 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] . . .

  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 ] [ 700] . . . 28474635 “Joe” 37814026 “Sam” 74856204 “Ann” An array of records

  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 ] [ 700] . . . 28474635 “Joe” 37814026 “Sam” 74856204 “Ann” An array of records

  13. Inserting a New Record ? new • 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)? 580625685 “Sarah” [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700] . . . 28474635 “Joe” 37814026 “Sam” 74856204 “Ann” An array of records

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

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

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

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

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

  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

More Related