Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why Hash table time complexity is o(1)

if hashtables are arrays under the hood, then why its time complexity is o(1) for insert and delete operations. will it be stored as free memory slots rather than back to back memory slots? why we are not considering that copy operation for hashtable itself?

I tried searching for some online references but no significant answer is found.

like image 270
K.Kaur Avatar asked May 22 '26 04:05

K.Kaur


1 Answers

Most of the hash table implementations have O(1) complexity on inserts and deletes in what called amortized time. That means that occasionally an operation might indeed take large amount of time (e.g. order of magnitude N - the number of elements in the table), but on any given infinite sequence of insert/delete queries average amount of operations per query will be limited by a constant. This is similar to a simple dynamic array's inserts being O(1) in amortized time even though occasionally an insert will cause full copy of an array which is definitely not a constant time operation.

like image 76
Alexander Pivovarov Avatar answered May 24 '26 15:05

Alexander Pivovarov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!