http://qs321.pair.com?node_id=694321


in reply to find closest element of array without going over

sadfaceman writes:

"....if I am going to repeatably perform this function."
I don't know if any of us saw that little phrase on the first read through. I missed it totally yet it could be important. If you are going to repeatedly search the same list then storing the list as Binary Tree would be a good optimization. You only build in once and you get the benefit every time you use it. The tradeoff is you have to write methods for creating the tree and traversing the tree.

NetWallah's method is similar in that he builds a sparse representation of the data to allow easy indexing. A Binary Tree is similar in method but the storage is more compact.

Update: Creating a binary tree has the added advantage of sorting the key array as you build the tree.


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}