Week 14 lec 1 Hashing
Week 14 lec 1 Hashing
Dr. Sohail
Week 14 Feb ..17
Objectives
• Today’s lecture objectives include:
– Hashing
• O(n) is a notation used in Big O notation, which describes the time complexity of an algorithm in
terms of input size n.
• When an algorithm has a time complexity of O(n), it means that the time required to complete the
algorithm grows linearly with the input size. In other words, if you double the input size, the runtime
will roughly double as well.
•O(log n). Logarithmic Time, The execution time grows slowly as input size increases, Common
in binary search algorithms. Efficient for large inputs, often seen in sorted data searching
• O(n²) – Quadratic Time. Execution time grows quadratically with input size. Common in
nested loops, Example (Bubble Sort), Bad for large inputs since time increases exponentially
• O(1) – Constant Time, The execution time does not depend on the input size.
def get_first_element(arr):
return arr[0] # Always takes the same time, regardless of array size
– Using an array of size 10,000 would give O(1) access time but will
lead to a lot of space wastage.
1. Deterministic – The same input always produces the same hash value.
4. Minimizes Collisions – Different inputs should ideally have different hash values.
5. Fixed Output Size – Regardless of input size, the hash value should be of a fixed
length.
Handling Collisions
• Collisions occur when two keys produce the same hash value.
• Common resolution techniques include:
1. Chaining (Separate Chaining) – Use linked lists at each index.
2. Open Addressing (Linear Probing, Quadratic Probing, Double Hashing) – Find
another available slot within the array.
Handling Collisions
•Separate Chaining: Store multiple values at the same index using linked lists.
•Linear Probing: Place 15 in the next available index (6), and 40 at the next (1).
Mid-Square
• The key is squared and the middle part of the result taken as the
hash value.
• To map the key 3121 into a hash table of size 1000, we square it
31212 = 9740641 and extract 406 as the hash value.
Folding
• It involves splitting keys into two or more parts and then combining
the parts to form the hash addresses.
6. Radix Conversion
• Transforms a key into another number base to obtain the hash
value.
5535410 = 3865211
Name ID h(r) = id % 13
Ziyad 985926 6
Ahmad 970876 10
Mahtab 980962 8
Saad 986074 11
Adnan 970728 5
Yousuf 994593 2
Husain 996321 1
0 1 2 3 4 5 6 7 8 9 10 11 12
• Database systems
• Symbol tables
• Data dictionaries