views:

80

answers:

3

I need a data structure that can insert elements and sort itself as quickly as possible. I will be inserting a lot more than sorting. Deleting is not much of a concern and nethier is space. My specific implementation will additionally store nodes in an array, so lookup will be O(1), i.e. you don't have to worry about it.

+2  A: 

Just use one of the self-balanced binary search trees, such as red-black tree.

squadette
I was wondering if there's something faster, plus I'd like to manually balance/sort.
someguy
If you want it sorted after every insert, with an arbitary number of elements so that you can't just have buckets for each item, then a tree is the way to go. That inserts you aren't going to get much faster than that I'm afraid.
thecoop
+2  A: 

If you're inserting a lot more than sorting, then it may be best to use an unsorted list/vector, and quicksort it when you need it sorted. This keeps inserts very fast. The one1 drawback is that sorting is a comparatively lengthy operation, since it's not amortized over the many inserts. If you depend on relatively constant time, this can be bad.

1 Come to think of it, there's a second drawback. If you underestimate your sort frequency, this could quickly end up being overall slower than a tree or a sorted list. If you sort after every insert, for instance, then the insert+quicksort cycle would be a bad idea.

P Daddy
@"although I'm not sure it needs to be balanced, as he suggests, since lookups are not much of an issue" Won't inserting be quicker if it's balanced? P.S. By "lookup" I don't mean search.
someguy
@someguy: Well, I suppose that depends on how much balancing overhead actually occurs, and how much traversal it prevents.
P Daddy
A: 

If you can do a lot of inserts before each sort then obviously you should just append the items and sort no sooner than you need to. My favorite is merge sort. That is O(N*Log(N)), is well behaved, and has a minimum of storage manipulation (new, malloc, tree balancing, etc.)

HOWEVER, if the values in the collection are integers and reasonably dense, you can use an O(N) sort, where you just use each value as an index into a big-enough array, and set a boolean TRUE at that index. Then you just scan the whole array and collect the indices that are TRUE.

You say you're storing items in an array where lookup is O(1). Unless you're using a hash table, that suggests your items may be dense integers, so I'm not sure if you even have a problem.

Regardless, memory allocating/deleting is expensive, and you should avoid it by pre-allocating or pooling if you can.

Mike Dunlavey