I am planning to implement binary search using arrays - however I also need to support dynamic updates / inserts into the array. The best approaches I've heard so far are in O(n) territory because all elements to the right of the insert point needs to be shifted to the right by one.
I pondered about this idea: I redefine / intercept (or "overload" depending on your language) the insertion method - if insertion into index 10 is detected for example, I "delay" this insertion, I keep the new element on a second list. Nothing is inserted in the real array, but the algorithm remembers the insertion point. Same for other insertions. Then if I detect an access to element 15, which I also intercept, I recalculate, offset the index by 1 to the right, and give the element 16 because there was an insertion into a location to the left of i=15.
Once in a while I would empty the second list by truly inserting them in the real list, possibly through insertion sort.
This sounds to me like it could work fast.
Would this be workable?
Thanks,
I'm pretty sure there's nothing really good in the direction you're looking. I've done things like that in the past, but only when there was a known access pattern that made it efficient.
If you want to keep some of the efficiencies of array storage and binary search, while still allowing dynamic inserts, a B+ tree is a good tradeoff:
https://en.wikipedia.org/wiki/B%2B_tree
In a B+ tree, each node has an array of keys. You choose the max length -- typically anywhere from 16 to 4096. The array storage limits waste because you don't have a whole bunch of extra links, helps cache locality during searches, and provides a high fanout that limits depth of the tree.