There are ranges of numbers, for example, from 84131840 to 84140031 and there are quite a lot of such ranges (about 100) if you need to check if this number is in any of all the ranges. Is there an option other than a loop with a separate comparison for each band?
- spans intersect? Can I sort them by start? - Mikhail Vaysman
- @Mikhail Vaysman in general, these numbers are the result of the work of the ip2long function; they do not overlap, but perhaps some of them belong to others. I don’t know how to check whether the range is in the range ... - marrk2
- @ marrk2 And what is the cycle did not please? 100 iterations for modern servers - this is not serious - Dmitriy Simushev
|
1 answer
Sort them at the beginning and use binary search. Search efficiency will be approximately O (log n).
- It is not the answer to the question. To leave your comments or ask the author to clarify, leave a comment to the appropriate post. - AK ♦
- one@AK and what would be the answer to the question? - Mikhail Vaysman
- Well, like that - a code that implements what you have written in words. - AK ♦
- @MikhailVaysman, does this decision make sense? It is, of course, beautiful, but it seems to me that in this case it complicates the task. There was no question about how to speed up the search. At least, this was not the case. As it seemed to me, the author wanted to find out how to not manually register all these numbers in comparison. And write a binary tree instead ... - Matty
- @Matty author asked about iterating over all elements in the loop. I suggested sorting the elements, not building the tree. - Mikhail Vaysman
|