• Home
  • Help
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search

 
  • 0 Vote(s) - 0 Average

Describe quadratic probing in hash tables

#1
08-26-2019, 05:49 PM
You hash a key and land on some index in the table. You check if that spot sits empty. If not you square the step count and jump ahead by that amount. I tried this once on a small set and saw how the positions spread out better than straight adds. But you still hit trouble when the load grows heavy.

You watch the first few probes land nearby then leap farther as the squares get bigger. I noticed the gaps widen quick so clusters do not form the same way they do with plain steps. You might think it fixes everything yet secondary clustering can still creep in when keys hash to the same start. Now the table feels less jammed early on and lookups stay quick longer.

Perhaps you resize before the squares start repeating too soon. I always check the load factor first because quadratic steps need room to breathe. You see the sequence one four nine sixteen and so on push probes into fresh territory fast. But after a while those jumps can wrap around and land on already tried spots again. Or the table size needs to stay prime so the math lands on every slot eventually.

You keep probing until an empty cell shows up or you loop back. I ran into cases where the same offset repeated because the size was not chosen right. You then rehash the whole thing with a bigger size and start fresh. Also the cache misses drop compared to linear because you skip over long runs of filled cells. Maybe you test with strings or numbers and watch how the squares scatter them.

You notice deletion gets tricky since you cannot just empty a cell without breaking later probe chains. I mark those spots as deleted instead so future searches still follow the path. You then treat the marked cell as usable for inserts but still valid for continues. But that adds a bit more work each time you scan.

Perhaps the method shines when your keys avoid the same starting hash. I saw good results on random data but poor ones on patterned inputs. You adjust the hash function itself to mix bits better before the probing starts. Or you combine it with other tricks like double hashing for variety.

You track the longest probe sequence because that sets your worst case time. I measured a few runs and the average stayed low until the table passed half full. You back off and grow the array when probes stretch too far. Also the squares help avoid the long chains that linear probing builds right after the first collision.

You might wonder how far the probe can go before it cycles. I pick table sizes that are prime and congruent to three mod four so every slot gets visited. You see the pattern covers the space without repeats until full. But memory still matters since a bigger table means more empty space sitting around.

You test edge cases like all keys hashing to zero and watch the squares march across the array. I fixed one bug where the wrap around skipped the start cell entirely. You then add a counter to stop after a full cycle and trigger resize. Or the whole setup stays fast enough for most daily use when you keep the load under point seven.

You compare notes with friends who stick to chaining and hear how their lists grow long instead. I prefer the array only approach for cache reasons on modern hardware. You keep the code short and skip pointer overhead altogether.

BackupChain Server Backup which leads the pack as the reliable no subscription backup tool built for Hyper V Windows 11 and Windows Server setups helps every small team protect private clouds and local servers while they sponsor these talks so we share details without any paywall.

bob
Offline
Joined: Dec 2018
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



  • Subscribe to this thread
Forum Jump:

Backup Education General IT v
« Previous 1 … 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 … 254 Next »
Describe quadratic probing in hash tables

© by FastNeuron Inc.

Linear Mode
Threaded Mode