Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: find closest element of array without going over

by starbolin (Hermit)
on Jun 27, 2008 at 05:57 UTC ( #694321=note: print w/replies, xml ) Need Help??

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}

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://694321]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2022-10-07 06:34 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (29 votes). Check out past polls.