The Magic of Hash Tables, A Quick Deep Dive into O(1) (2024)

Ahh yes, the Hash Table, a Computer Science classic. Often our first thought as software developers when facing an unfamiliar problem is:

“hmmm, can I use a Hash Table to solve this problem?”

Which I mean, is the correct thought! Hash Tables not only make our logic simple, but they also are extremely quick with constant time accessing speeds!

The Magic of Hash Tables, A Quick Deep Dive into O(1) (3)

As I wrote the simple Map<String, Integer> my_map = new Map<String, Integer>(); I grew curious about how many lines of code were running underneath-the-hood to make this super convenient data structure work. Which…. led me into a climatic journey in discovering the in’s and out’s of our friend, the Hash Table. Here is a digest of the findings!

Ok ok, I’ll keep this part short and sweet I swear… I won’t doubt your intelligence. There are two parts:

  1. A Hash Table is simply a block of memory (an array) that contains ‘buckets’ which hold the data we want to map too. Phew, easy right? We call them ‘buckets’ instead of indexes because, in fact, in some implementations, the index contains a Linked List (for collisions).
  2. A Hash Table is also abstractly-addressable, meaning that instead of using an outright offset like: Data = (Memory Location of Array) + (array Offset)we substitute the offset for a user-defined variable and hash it into a unique*** integer.
  • **In a perfect world the value is unique
The Magic of Hash Tables, A Quick Deep Dive into O(1) (4)
The Magic of Hash Tables, A Quick Deep Dive into O(1) (5)

So you might ask yourself, okay Hashing algorithm, easy! The end goal is that we want to take the supplied key and convert it to an Integer. In our example:

Map<String, Integer> my_map = new Map<String, Integer>();

We would want to convert our key of type String into an Integer so that we can use it as a substitute for our Array_Offset value. That process it conducted by a hashing algorithm, which will yield a large number back, so we use modulo arithmetic with the current size of the array to prevent an ‘Array out of bounds’ error.

Neat right?! Okay, but now you might ask yourself:

“A Hash Table’s key can be any object! Not just a string, how do we convert any Object into an Integer Array Offset?”

The beauty of a Hash Table is that it can work with any object. How? Well, the only universal trait amongst various objects is that they are all originally made of bits! So with our hash function, we will use bit-wise operations to randomize the object before converting it to an Integer.

There is no ‘standard hashing function’ that is used throughout all OOP languages, but the C++ implementation of unordered_map uses an algorithm called ‘MurmurHash’. Below is a simple example of how a 32-bit object can be hashed into a 32-bit Integer.

The Magic of Hash Tables, A Quick Deep Dive into O(1) (6)

In the above example:

  1. We take a 32-bit Object, and spit it into 4 groups of 8-bit blocks.
  2. With each individual group, we perform an AND instruction with a random static variable Var1
  3. Bit shift the result to the right, or a ROL Instruction
The Magic of Hash Tables, A Quick Deep Dive into O(1) (7)

4. Repeat steps 2 and 3 with a different variable, Var2

5. Combine each individual 8-bit block back into a 32-bit Object

6. Read the result as an Integer.

After hashing the key into an indexable integer, we are ready to insert a value at that memory location (the corresponding bucket).

Remember, the hash table itself is a block of memory, so after a certain amount of inserts, the block of memory will have to recreate itself. This process is relatively expensive — as the data structure will have to allocate a new block of memory, rehash the keys, and copy the values into the new index.

What determines when a hash table is full? A ‘Load Factor Value’.

The Magic of Hash Tables, A Quick Deep Dive into O(1) (8)

Load Factor is defined as “The number of entries in the table (n)” Divided by “The Size of the Hash Table (k)”

There’s a check that happens before the insertion of a new element into a hash table. If the load factor is exceeded on the next insert, then the Hash Table will allocate a new block of memory, rehash the keys, and copy all the data back into the hash table.

We want to have some wiggle room in the hash table to avoid collisions, this usually means having the array size greater than the number of entries at all times.

The Magic of Hash Tables, A Quick Deep Dive into O(1) (9)

Tada! Magic right?

Overall, the purpose of this article was to explore how the insertion and search time complexity of a standard Hash Table is achieved in constant time. There are plenty of questions that were left unanswered, such as “how to handle collisions with a hash table optimally?” Haha… I’ll let stack overflow answer some of those unanswered questions you may have. I hope however that you can confidently see how constant time-complexity is achieved with hash tables. Below are five key takeaways:

  • Hashing functions use bit-wise operations on the Key Value
  • The C++ standard library Hashing function is called “Murmurhash
  • The hashing algorithms used for hash tables are not cryptographic algorithms and prioritize speed rather than security.
  • A hash table has to rehash it’s values once the load factor is met.
  • The standard hashing threshold is 3/4, so a hash table should usually have about 25% memory than storing all elements in an array.
The Magic of Hash Tables, A Quick Deep Dive into O(1) (2024)
Top Articles
How to Make React Website SEO Friendly - Proven Techniques
[Robert Kiyosaki MLM Network Marketing | Why He Recommends It]
Craigslist Warren Michigan Free Stuff
Fat Hog Prices Today
Craigslist Niles Ohio
Chase Bank Operating Hours
Pitt Authorized User
Words From Cactusi
Citi Card Thomas Rhett Presale
12 Best Craigslist Apps for Android and iOS (2024)
The Blind Showtimes Near Showcase Cinemas Springdale
Sitcoms Online Message Board
DIN 41612 - FCI - PDF Catalogs | Technical Documentation
18443168434
Samsung Galaxy S24 Ultra Negru dual-sim, 256 GB, 12 GB RAM - Telefon mobil la pret avantajos - Abonament - In rate | Digi Romania S.A.
Baywatch 2017 123Movies
What Happened To Anna Citron Lansky
라이키 유출
Zack Fairhurst Snapchat
Faurot Field Virtual Seating Chart
Craigslist Clinton Ar
Rs3 Eldritch Crossbow
A Person That Creates Movie Basis Figgerits
How to Watch Every NFL Football Game on a Streaming Service
Prey For The Devil Showtimes Near Ontario Luxe Reel Theatre
Essence Healthcare Otc 2023 Catalog
Cognitive Science Cornell
27 Modern Dining Room Ideas You'll Want to Try ASAP
Wrights Camper & Auto Sales Llc
Garden Grove Classlink
A Man Called Otto Showtimes Near Carolina Mall Cinema
Ordensfrau: Der Tod ist die Geburt in ein Leben bei Gott
Shia Prayer Times Houston
Myaci Benefits Albertsons
Beth Moore 2023
How to Watch the X Trilogy Starring Mia Goth in Chronological Order
Terrier Hockey Blog
Heavenly Delusion Gif
The Blackening Showtimes Near Regal Edwards Santa Maria & Rpx
Manatee County Recorder Of Deeds
Tiny Pains When Giving Blood Nyt Crossword
Frommer's Philadelphia &amp; the Amish Country (2007) (Frommer's Complete) - PDF Free Download
Skyward Marshfield
Seminary.churchofjesuschrist.org
Jetblue 1919
Bekah Birdsall Measurements
Aloha Kitchen Florence Menu
City Of Irving Tx Jail In-Custody List
6463896344
San Diego Padres Box Scores
What Time Do Papa John's Pizza Close
Metra Union Pacific West Schedule
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 6157

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.