09-03-2009 10:08 AM
Today I'll throw out a VI that I happened to discover on a random walk through the palettes. The VI is "Search Ordered Table.vi". Is it just me, or is it vaguely similar to the "Threshold 1D Array" function (only slightly less functional)? Is there something we should know about one or the other? If you check that it returns an integer index, perhaps you have an efficient search method for a sorted array.
Groundrules for VIOTD here.
09-03-2009 11:17 AM
I believe that threshold array does a linear search, finding the first threshold, while search ordered table does a binary search (after some poorly explained hunting phase, which I don't quite "get"). A binary search is significantly faster for very large arrays, but requires a sorted array.
(For a related discussion, see also my suggestion here, and vote for it if you like it :))
Presumably, the picture shows four array values as a function of the array index. Obviously, the array shown is incorrect, because the first element is higher than the second, in direct contradiction to the requirement that the function needs a sorted array input. 😮
(I'll report this in the bug thread...)
09-03-2009 11:23 AM
09-03-2009 11:44 AM
should they not be ordered in size from smallest to largest (# of elements)?
The icon surely does not depict that..
09-03-2009 11:52 AM
I also had a hunch that threshold array was not doing what (I think) it should. Why would you go out of your way to require a sorted input array just to turn around and perform a linear search? Might as well simply return the first match.
I like the suggetion of a binary search function, even if it is separate. I am waiting to vote for it until I make peace with the idea of '-1' being used to signify no match...
Bonus question now: What happens in both cases when you input a non-sorted array?
09-03-2009 12:12 PM
09-03-2009 01:38 PM - edited 09-03-2009 01:43 PM
Darin.K wrote:I also had a hunch that threshold array was not doing what (I think) it should. Why would you go out of your way to require a sorted input array just to turn around and perform a linear search?
Threshold array does NOT require a sorted input, but it will only find the first place after "index" where the threshold is reached. This, together with the index input allows for some fancy and useful code.
Here's one old example how it could be used.
ALSO:
I suggest to get rid of search ordered table and instead allow an "array is sorted" boolean input for threshold array, similar to my suggestion for search array. (stay tuned for another idea post ;)). It would avoid duplicate functionality. Search ordered table is relatively
An enhanced "threshold array" would work for everything, eliminating duplicate tools such as "sort ordered table".
09-03-2009 01:56 PM - edited 09-03-2009 01:57 PM
Darin.K wrote:I am waiting to vote for it until I make peace with the idea of '-1' being used to signify no match...
I guarantee you that you will get strong opposition if you would suggest to get rid of the "-1" to signify the absence of an answer. It's everywhere!!! 😄
I32 does not have NaN! What do you suggest? Make the output an array with zero or one I32 elements? Empty array for "not found"? Add an error output that goes high for "not found"? Not satisfactory!
09-03-2009 01:59 PM
altenbach wrote:
Darin.K wrote:I also had a hunch that threshold array was not doing what (I think) it should. Why would you go out of your way to require a sorted input array just to turn around and perform a linear search?
Threshold array does NOT require a sorted input, but it will only find the first place after "index" where the threshold is reached. This, together with the index input allows for some fancy and useful code.
An enhanced "threshold array" would work for everything, eliminating duplicate tools such as "sort ordered table".
Message Edited by altenbach on 09-03-2009 11:43 AM
Require is probably the wrong word, I was just pointing out the ominous warning in the help:
Note Use this function only with arrays sorted in non-descending order.
I assume you meant "Search Ordered Table", "Sort Ordered Table" would be an interesting VI. Better not use "Quicksort" I seem to remember that a sorted array is a worst-case-scenario for the simple implementations.![]()
09-03-2009 02:05 PM