The integers 5, 26, 22, 41, 18, 16, 4, 13 are to be stored in a closed bucket hash table of length 13: i. Suggest a suitable hash function. ii. Compare the efficiency of (i) and (ii). iii. Give the time complexity of various sorting/searching algorithms as per the table given below: | Algorithm | Best case | Average case | Worst case | | :--- | :--- | :--- | :--- | | Binary search | - | - | - | | Insertion sort | - | - | - | | Linear search | - | - | - | | Bubble sort | - | - | - | | Radix sort | - | - | - | | Quick sort | - | - | - | | Heap sort | - | - | - | | Merge sort | - | - | - |


Q.) The integers 5, 26, 22, 41, 18, 16, 4, 13 are to be stored in a closed bucket hash table of length 13: i. Suggest a suitable hash function. ii. Compare the efficiency of (i) and (ii). iii. Give the time complexity of various sorting/searching algorithms as per the table given below:

Algorithm Best case Average case Worst case
Binary search - - -
Insertion sort - - -
Linear search - - -
Bubble sort - - -
Radix sort - - -
Quick sort - - -
Heap sort - - -
Merge sort - - -
Subject: Data Structures

i. Suggest a suitable hash function.

For a hash table of length 13, a suitable hash function would be one that distributes the integers evenly across the 13 buckets to minimize collisions. A simple hash function that could work well in this case is the modulo operation:

hash(x) = x mod 13

This hash function takes an integer x and returns the remainder when x is divided by 13. Let's apply this hash function to the given integers:

Integer Hash Function (x mod 13) Hashed Value
5 5 mod 13 5
26 26 mod 13 0
22 22 mod 13 9
41 41 mod 13 2
18 18 mod 13 5
16 16 mod 13 3
4 4 mod 13 4
13 13 mod 13 0

As you can see, there are collisions for the hashed values 0 and 5. In a closed bucket hash table, these collisions would be resolved using a collision resolution technique such as linear probing, quadratic probing, or double hashing.

ii. Compare the efficiency of (i) and (ii).

Since there is no (ii) provided, I will assume you are asking to compare the efficiency of the hash function with and without collision resolution.

Without collision resolution, the hash function's efficiency is poor because multiple integers map to the same bucket, causing collisions. This means that if we try to insert or search for an integer, we may have to deal with the additional complexity of handling these collisions.

With collision resolution, the efficiency of the hash function improves as we have a systematic way to handle collisions. However, the efficiency also depends on the load factor (the number of elements divided by the number of buckets) and the chosen collision resolution technique.

iii. Give the time complexity of various sorting/searching algorithms as per the table given below:

Algorithm Best case Average case Worst case
Binary search O(1) O(log n) O(log n)
Insertion sort O(n) O(n^2) O(n^2)
Linear search O(1) O(n) O(n)
Bubble sort O(n) O(n^2) O(n^2)
Radix sort O(nk) O(nk) O(nk)
Quick sort O(n log n) O(n log n) O(n^2)
Heap sort O(n log n) O(n log n) O(n log n)
Merge sort O(n log n) O(n log n) O(n log n)
  • Binary search is efficient for sorted arrays, with a best case when the target is at the middle (O(1)), but typically it takes O(log n) time.
  • Insertion sort is efficient for small or nearly sorted arrays, with a best case of O(n) when the array is already sorted, but it has a quadratic time complexity on average and in the worst case.
  • Linear search goes through each element until it finds the target, with a best case when the target is at the beginning, but on average and in the worst case, it takes O(n) time.
  • Bubble sort has a best case of O(n) when the array is already sorted, but it is inefficient with an average and worst-case time complexity of O(n^2).
  • Radix sort is a non-comparative integer sorting algorithm that takes O(nk) time, where n is the number of elements and k is the number of digits in the largest number.
  • Quick sort is a divide-and-conquer algorithm with an average and best-case time complexity of O(n log n), but it can degrade to O(n^2) in the worst case if the pivot selection is poor.
  • Heap sort is a comparison-based sorting algorithm with a time complexity of O(n log n) in all cases.
  • Merge sort is a divide-and-conquer algorithm with a stable time complexity of O(n log n) in all cases.

Note: The time complexities provided are for comparison-based sorting algorithms. Non-comparison sorts like Radix sort have different time complexities based on other factors (e.g., the range of the numbers being sorted).