4

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.

2
  • If you don't have to use an array specifically, take the index, wrap it in an object and use it as a Hash key. Then you still can only have one object with that index, and you don't have too many arbitrarily large buckets. Commented Feb 18, 2010 at 16:16
  • FWIW, You might find some of the discussion on my question here interesting: stackoverflow.com/questions/2267331/… Commented Feb 18, 2010 at 17:25

4 Answers 4

3

Why not just use a hashtable / dictionary? If you really need something this specific, the first thing that comes to mind for me is a B tree. But there's probably much better solutions than that too.

Sign up to request clarification or add additional context in comments.

Comments

3

I believe you are looking for a hashmap or more generally a map. You can use the STL provided map class.

This sounds like exactly what you are looking for:

http://www.cplusplus.com/reference/stl/map/

3 Comments

I don't know who downvoted us, but I gave yours back. I don't think it's fair without explanation.
Thanks I did the same for you as it seems we saw the question similarly.
I guess some people view a map as an "anti-array" but this is definitely the right answer for C++.
2

Maybe what you want is a sparse vector? Try the Boost implementation.

Comments

1

You're looking either to use sparse arrays or some sort of hash, depending on circumstances. In general:

  1. If you're going to eventually end up with long runs of filled buckets separated by large gaps, then you're better off with a sparse array, as they optimize memory use well in this situation.
  2. If you're going to just end up with scattered entries in a huge sea of empty holes, you're better off with a hash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.