# Time complexity analysis for Searching in a Hash table

I want to analyse the time complexity for Unsuccesful search using probabilistic method in a Hash table where collisions are resolved by chaining through a doubly linked list. And the doubly linked list is kept in sorted order (Ascending).

I proceeded in the following way: We will stop if the element in the linked list is greater than the element to be searched, otherwise continue the search. I can understand that this will be better than the normal Unsuccesful search in Hashing i.e. $$O(\alpha + 1)$$. But how do I do exact analysis of average case time complexity in this case using Expectations in Probability.

Edit: The Keys are randomly distributed from 1 to n and the number of slots in Hash table are m.

• Is anything known about the distribution of data? If not, then I don't think you can do anything better than concluding O(loadFactor+1) Sep 21, 2022 at 9:23
• @RinkeshP, The keys are distributed randomly between 1 and n. Sep 21, 2022 at 9:44
• Even with this information, you gain no insight, as the element you are searching might be greater than the last element(greatest) of its chain. Until you know how your data within a chain is distributed(other than being a uniform random distribution) I doubt this can be taken any further. Sep 21, 2022 at 9:58
• Yes that is the worst case, however I need a bound for average time analysis. Sep 21, 2022 at 10:00

Let $$\alpha = \frac{n}{m}\$$ where $$n$$ = total no. of elements in hashtable ,$$m$$ = number of slots in hashtable, be the load factor or Average number of elements in a chain.

Given that each chain is sorted, an unsuccessful attempt at search would require $$1$$ comparison in the best case and $$\alpha$$ comparisons in the worst case. The expected no. of comparisons for the same is

$$\frac{1}{\alpha }(1+2+3+....+\alpha) = \frac{\alpha (\alpha +1)}{2\alpha } = \frac{\alpha +1}{2}$$

yielding an average case complexity of $$O(\frac{\alpha +1}{2}+1)$$.

P.S. If your chains are sorted then you can lower the complexity to $$O(\log_{2}\alpha+1)$$ by using binary search(depending on your implementation of linked list)

• You can't perform a binary search in a linked list. (And the length is not $n$.)
– user16034
Sep 21, 2022 at 10:49
• Implementation details, if its implemented as a typical "linked list" then no. if its simply an array then yes. Sep 21, 2022 at 10:50
• The question explicitly says linked list. But how would you organize the collision lists in an array ?
– user16034
Sep 21, 2022 at 10:53
• Linked list as an abstraction. I simply provided a possibility where the implementation could be an array implementation(the hashtable is a 2d array). Similarly collisions could be handled by using binary search tress as well. Sep 21, 2022 at 10:57
• Yves, you can implement a linked list as one or more arrays, which may give you actually better performance for large linked lists. However, the linked lists for overflow of a hash table are supposed to be small. If they are not small, you'd increase the number of slots in the hash table which will give you much better results. Sep 21, 2022 at 11:30