Data Structures Using C++ 2E: Searching and Hashing Algorithms
Data Structures Using C++ 2E: Searching and Hashing Algorithms
Chapter 9
Searching and Hashing Algorithms
Hashing (cont’d.)
• Overflow and collision occur at same time
– If r = 1 (bucket size = one)
• Choosing a hash function
– Main objectives
• Choose an easy to compute hash function
• Minimize number of collisions
• If HTSize denotes the size of hash table (array size
holding the hash table)
– Assume bucket size = one
• Each bucket can hold one item
• Overflow and collision occur simultaneously
• Overflow
– No longer a concern
• Data stored in linked lists
• Memory space to store data allocated dynamically
– Hash table size
• No longer needs to be greater than number of items
– Hash table less than the number of items
• Some linked lists contain more than one item
• Good hash function has average linked list length still
small (search is efficient)
• Advantages of chaining
– Item insertion and deletion: straightforward
– Efficient hash function
• Few keys hashed to same home position
• Short linked list (on average)
– Shorter search length
• If item size is large
– Saves a considerable amount of space
• Load factor
– Parameter α
• Hash functions
– Mid-square
– Folding
– Division (modular arithmetic)
• Collision resolution technique categories
– Open addressing (closed hashing)
– Chaining (open hashing)
• Search analysis
– Review number of key comparisons
– Worst case, best case, average case
Data Structures Using C++ 2E 21
Data Structures Using C++ 2E
Chapter 10
Sorting Algorithms
Objectives
• Search algorithms
– Concerned with number of key (item) comparisons
• Sorting algorithms
– Concerned with number of key comparisons and
number of data movements
• Analysis of selection sort
– Function swap
• Number of item assignments: 3(n-1)
– Function minLocation
• Number of key comparisons of O(n2)
FIGURE 10-5 list elements while moving list[4] to its proper place
FIGURE 10-8 list elements while moving list[4] to its proper place
FIGURE 10-13 Linked list after moving the node with info 8 to the beginning