0

I am looking for an implementation of a O(log n) array search algorithm. The array is numeric and sorted [[1,2],[3,5],[4,5],...]. Because it's a on mouse over user interaction it needs to be super fast. The function should get the first array element to search and return the second of the found element. I can implement recursive quicksearch myself, but what existing solutions are out there? Is the Array.find somehow efficient for instance?

Edit: I tried binary trees for instance, but the lag is still noticeable.

El Dude
  • 5,328
  • 11
  • 54
  • 101
  • 2
    I don’t really understand what the algorithm is supposed to do. Can you give us an example? – Sebastian Simon May 30 '17 at 00:11
  • 4
    Do you mean: the function receives, as an argument, a number; the algorithm searches the whole array for an array where the first element is that number; it should then return the second number in that found array? Why not use an object instead? `var obj = {1: 2, 3: 5, 4: 5, …};`, then access the value with `obj[1]` to receive `3`. That’s going to be faster than any function call. – Sebastian Simon May 30 '17 at 00:16
  • 3
    Please try things first...then when you run into problems ask questions then. How to search for a value in an array is not hard to research. Using an object would definitely be faster depending on what you are looking for. Whole question is too broad – charlietfl May 30 '17 at 00:20
  • Also, O(log) doesn’t mean anything. [Property lookup can be assumed to be O(1)](https://stackoverflow.com/q/7700987/4642212). Assuming you actually mean O(log _n_), do you really _specifically_ need an algorithm with that exact speed? What have you tried so far? – Sebastian Simon May 30 '17 at 00:42
  • Also note: on small to medium sized arrays, optimized binary search is still slower than simple O(n) lookup – le_m May 30 '17 at 01:30
  • 1
    I used https://www.npmjs.com/package/binary-search for a quick binary search impl. in JavaScript. – Peheje May 30 '17 at 17:35

0 Answers0