How can worst case time complexity of Hashtable is O (n^2)

Source: Internet
Author: User
Tags rehash

Hash Tables was O(1) average and amortized case complexity, however was suffers from O(n) worst case Tim E complexity. [and I think this was where your confusion is]

Hash tables suffer from O(n) worst time complexity due to both reasons:

    1. If too many elements were hashed into the same key:looking inside this key is take time O(n) .
    2. Once A hash table have passed its load balance-it have to rehash [create a new bigger table, and re-insert each element to The table].

However, it's said to being O(1) average and amortized case because:

    1. It's very rare that many items would be hashed to the same key [if you chose a good hash function and you don ' t had too b IG load balance.
    2. The rehash operation, which is O(n) , can at most happen after n/2 OPS, which be all assumed O(1) : Thus if you sum th E Average Time per op, you get:(n*O(1) + O(n)) / n) = O(1)

Note because of the rehashing issue-a realtime applications and applications, need low latency-should not use a ha SH table as their data structure.

EDIT: Annother issue with Hash Tables:cache
Another issue where you might see a performance loss in large hash tables are due to cache performance. Hash Tables suffer from bad cache performance, and thus for large collection-the access time might take longer, Since you need to reload the relevant part of the table from the memory back into the the cache.

How can worst case time complexity of Hashtable is O (n^2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.