02-14-2011 04:14 PM
What better day to think about splitting up (ie. bisection) than Valentine's Day. It is a well-known fact that the search algorithm utilized by Search 1D Array will not take advantage of situations where the input array is sorted. So what? The bisection method for searching an ordered list can complete in O(log2(N)) steps as opposed to O(N). With large lists, this can be quite dramatic, especially when you are searching many times. Someday this oversight may be remedied, until then, there is a useful tool for searching 1D DBL arrays which implements the bisection method:
\vi.lib\ptbypt\Other Functions.llb\BinarySearch.vi
Once again, love the icon, but have a few quibbles with the code. As much as I dislike the standard behavior, the norm is to return -1 when the value is not found, this VI returns 0 while relying on you to check the isFound boolean. For some reason, the mini-Rube divide by 2 followed by Round toward -Infinity is used where a simple Quotient & Remainder would do the trick (and keep things blue). Outside of that, this VI can really speed up your searching, and often it can be worthwhile to sort a list once to speed all future searching.
Once you have tried this out and are enjoying the results, try to inspire NI to give us a "real" function with all the polymorphic bells and whistles by voting here:
Side Note 1: I had never really noticed that the Sign function returns the same datatype as the input. I wouldn't mind an output configuration here, as driving a Case Structure is probably not that uncommon.
Side Note 2: The previous behavior got me thinking about complex data passed to the Sign function. Very interesting, gives you the corresponding point on the unit circle, ie. same phase unit amplitude complex value. I can see a few code mods in my future.
Side Note 3: Exploring complex numbers in the Sign function got me wondering: Why are there increment/decrement buttons on the complex number controls if they aren't allowed to increment or decrement anything? Same for the System spinner controls.
02-14-2011 04:59 PM
Side Note 1: I had never really noticed that the Sign function returns the same datatype as the input. I wouldn't mind an output configuration here, as driving a Case Structure is probably not that uncommon.
For a simple numeric, I use the cases 1, 0, -1, default. What other data types have you in mind (uncleare behaviour with complex numbers is already shown by you).
Well, I checked with 'singels' ('cause this is valentines day), and everyone knows that they are a bit on the side when it's about representing 0 and 1. But all worked fine.
Felix
02-14-2011 05:47 PM
@f. Schubert wrote:
Side Note 1: I had never really noticed that the Sign function returns the same datatype as the input. I wouldn't mind an output configuration here, as driving a Case Structure is probably not that uncommon.
My retentive side would prefer a signed integer (I32 say) when a SGL/DBL is wired to avoid that little red dot that happens with the Case Selector. Letting LV handle the dot is usually faster than dropping an explicit coercion, I would not mind if it were all swept under the rug and I could have the option of DBL in/I32 out.
03-08-2011 03:54 PM
You may also want to check out the "Search Ordered Table VI" which is in the Interp & Extrap VIs palette. This guy uses a "fast hunting" alogrithm to bracket the location followed by a bisection approach.
Someone looking for speed when searching an unsorted list may want to use the Red/Black tree alogrithm which is implemented in the Variant data type. See here for details :
http://forums.ni.com/t5/LabVIEW/Darren-s-Weekly-Nugget-10-09-2006/m-p/425269