I need some help writing an algorithm in C/C++ (Although any language example would work). The purpose is a container/array, which allows insertion at any index. However if inserting an element in an index that is not close to an existing index i.e. would cause an large empty space of buckets. Then the array would minimise the empty buckets.
Say you have a set of elements which need to be inserted at the following indexes:
14
54
56
57
12
8
6
5678
A contiguous array would produce a data structure. Something like this:
0
1
2
3
4
5
6 val
7
8 val
9
10
11
12 val
...
However, I'm looking for a solution that creates a new array when an index is not within x buckets of it's nearest neighbour.
Something like this:
Array1
6 val
7
8 val
10
11
12 val
13
14 val
Array2
54 val
56 val
57 val
Array 3
5678 val
Then use some kind of index map to find the array an index is in during a lookup. My question is what kind of algorithm should I be looking at to group the indexes together during inserts? (while still keeping a good space/time trade off)
Edit: Thanks for the answers so far. The data I'm going to be looking at will contain one or two very large index ranges with no gaps, then one or two very large gaps then possibly a couple of "straggling" single values. Also the data needs to be sorted, so hash tables are out.