2082 2

BIT201 ยท 2082 ยท 10 marks

Define primitive data type with example.What are the advantages of hashing?With your own example show the hash collision and how do you handle it? Explain.[2+2+6]

Define primitive data type with example.What are the advantages of hashing?With your own example show the hash collision and how do you handle it? Explain.[2+2+6]

Data Structures and AlgorithmsChapter 1: Fundamentals of Data Structures and AlgorithmsPrimitive data types with examples

Quick answer

Hashing is a technique of mapping keys to positions in a hash table using a hash function.

Primitive Data Type, Hashing, Collision and Collision Handling


1. Primitive Data Type (2 marks)

Definition: A primitive data type is a basic, built-in data type provided directly by a programming language. It is the most fundamental type of data that cannot be broken down into simpler data types. These types are predefined by the language and have a fixed size in memory.

Examples:

Data TypeDescriptionExample Value
intInteger numbers5, -20, 100
floatDecimal/floating point numbers3.14, -0.5
charSingle character'A', 'z'
booleanTrue or false valuetrue, false

For example, in C/Java: int age = 21; -- here int is a primitive data type storing a whole number.


2. Advantages of Hashing (2 marks)

Advantages:

  1. Fast Search (O(1) average): Hashing provides constant time average-case complexity for search, insert, and delete operations, which is much faster than linear search O(n) or binary search O(log n).

  2. Efficient Storage and Retrieval: Data can be stored and retrieved very efficiently using a key, making it ideal for dictionaries, databases, and caches.

  3. Direct Address Mapping: The hash function directly computes the storage location, avoiding sequential scanning.

  4. Useful for Large Data Sets: Even with a large number of records, hashing maintains near-constant performance.

  5. Easy Implementation of Symbol Tables: Compilers use hashing to implement symbol tables efficiently.


3. Hash Collision and Collision Handling (6 marks)

What is Hash Collision?

A hash collision occurs when two or more different keys produce the same hash value (index) using the hash function, meaning they map to the same location in the hash table.

My Own Example:

Hash Function:

h(key) = key mod 7

Hash Table Size = 7 (indices 0 to 6)

Keys to insert: 10, 20, 17, 24, 31

Keyh(key) = key mod 7Index
1010 mod 7 = 33
2020 mod 7 = 66
1717 mod 7 = 33 -- COLLISION with 10!
2424 mod 7 = 33 -- COLLISION again!
3131 mod 7 = 33 -- COLLISION again!

Keys 10, 17, 24, and 31 all hash to index 3 -- this is a hash collision.


Collision Handling Techniques

Method 1: Chaining (Open Hashing)

In chaining, each slot of the hash table holds a linked list. All keys that hash to the same index are stored in the linked list at that index.

After inserting 10, 20, 17, 24, 31 using chaining:

Index 0: NULL
Index 1: NULL
Index 2: NULL
Index 3: [10] -> [17] -> [24] -> [31] -> NULL
Index 4: NULL
Index 5: NULL
Index 6: [20] -> NULL

Advantages of Chaining:

  • Simple to implement
  • Hash table never becomes full
  • Deletion is easy

Method 2: Open Addressing - Linear Probing (Closed Hashing)

In linear probing, if a collision occurs at index i, we check the next slot i+1, then i+2, and so on until an empty slot is found.

Formula:

h(key, i) = (h(key) + i) mod TableSize
where i = 0, 1, 2, ... (probe number)

Inserting 10, 20, 17, 24, 31 using Linear Probing (Table Size = 7):

StepKeyh(key)ProbeFinal Index
1103i=0, slot 3 empty3
2206i=0, slot 6 empty6
3173i=0, slot 3 full; i=1, slot 4 empty4
4243i=0 full, i=1 full, i=2, slot 5 empty5
5313i=0 full, i=1 full, i=2 full, i=3, slot 6 full, i=4, slot 0 empty0

Final Hash Table:

Index 0: 31
Index 1: NULL
Index 2: NULL
Index 3: 10
Index 4: 17
Index 5: 24
Index 6: 20

Advantages of Linear Probing:

  • No extra memory needed for pointers
  • Better cache performance

Disadvantage:

  • Primary clustering -- long chains of filled slots can form, slowing down operations.

Summary Table

MethodIdeaHandles Full Table?
ChainingLinked list at each slotYes
Linear ProbingNext available slotNo (table can fill up)

Conclusion: Hash collision is unavoidable in practice, but with proper handling techniques like chaining or open addressing the table keeps giving almost constant time access. Chaining is preferred when the number of keys is not known in advance or when deletions are frequent, while linear probing suits a table of fixed size where memory for pointers is not available. In both cases the load factor should be kept low (roughly 0.7 or less for open addressing) so that the average number of probes stays close to one.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.