Data structure: sort and search effectively

data structuressearchsorting

I need to have a data structure with say 4 keys . I can sort on any of these keys. What data structure can I opt for? Sorting time should be very little.

I thought of a tree, but it will be only help searching on one key. For other keys I'll have to remake the tree on that particular key and then find it. Is there any data structure that can take care of all 4 keys at the same time?

these 4 fields [source ip, destination ip, source port, destination] are of total 12 bytes and total size for each record – 40 bytes.. have memory constraints too…

around one lac records

operations are : insertion, deletion, sorting on different keys.

For printing , sorting the records on any of one keys should not take more than 5 seconds.

Best Answer

1. If you rarely add and remove data

What about using the same technique as the one used in RDBMS with indexes?

In other words, you'll have the unordered set containing the data, and four ordered sets containing the keys and the pointers to the items in the data set.

Of course, this may cause performance issues if you need to frequently add and remove lots of data.

2. If data is added or removed frequently

You can slightly modify the algorithm to reduce the performance impact of sorting the four index sets every time you add or remove an item. You may, for example, have four unordered index sets, create from them the sorted sets when needed, and invalidate those sorted sets when an element is added or removed.

3. Profile

Note that profiling is important, since you can't possibly guess where the bottleneck will be. Remember than:

  • When you remove an item from the data set, removing four keys from four index sets is fast, since those sets are already ordered;

  • When you add an item, adding four keys to the index sets is not hugely slow: you just have to walk through the sets, and insert the keys at the appropriate position:

    Let the list be:

     3, 7, 8, 12, 16, 22, 23, 24, 27
    

    If you need to add the value 25, position yourself at the middle of the list:

     3, 7, 8, 12, 16, 22, 23, 24, 27
                  ↑
    

    Since 25 is greater then 16, go to the right:

     -, -, -, --, --, 22, 23, 24, 27
                             ↑
    

    And again to the right:

     -, -, -, --, --, --, --, 24, 27
                                 ↑
    

    Found the position.

Related Topic