简体   繁体   中英

What search algorithm or a data structure would you suggest?

I have a big number of SortedSet<Long> structures:

1, 2, 5, 8, 10, 35, 77, ...
5, 9, 35, 50, 132, ...
2, 4, 8, 15, 17, 23, ...
... hundreds of thousands of such rows...

I need to find a number that goes after, say, 50 . In this example (if there are just three sets) it is 77 . The number of sets is huge - hundreds of thousands. What algorithm would you suggest?

If I understand correctly here is my idea:

Collection<SortedSet<Long>> sets = //...

long minAfter50 = Long.MAX_VALUE;
for (SortedSet<Long> set : sets) {
    final Long first = set.tailSet(51L).first();
    minAfter50 = Math.min(minAfter50, first);
}

Here is the idea:

  • iterate over all input sets
  • crop all values less then or equal 50
  • take the first argument of the cropped set (guaranteed to be greater than 50)
  • calcualte the smallest value out of those collected in previous step

UPDATE (based on @beerbajay comment): if SortedSet is actually a TreeSet , the following code might perform better. Also I am making sure that there is any value greater than 50 in each and every set:

long minAfter50 = Long.MAX_VALUE;
for (TreeSet<Long> set : sets) {
    final Long higher = set.higher(50L);
    if (higher != null && higher < minAfter50) {
        minAfter50 = higher;
    }
}

If that's all the precomputation you allow, then the only thing you can do is call tailSet on each SortedSet and find the minimum.

If you allow some extra data structures, the easiest thing to do is keep track of the union of all the sets and then you just have to call tailSet on that.

I suspect neither is the answer you want. Perhaps you could better describe the constraints you have?

set is implemented as a binary search tree, the largest number always in the last. you can search number bigger than 50 easier, always get the 1st number bigger than 50 in each set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM