User | Kudos |
---|---|
11 | |
9 | |
3 | |
3 | |
2 |
Searching an array for a certain element can be expensive for large arrays. The speed could be dramatically increased if we can assume that the input array is sorted. The speed difference can be several orders of magnitude.
There is an old example that discusses this in more detail. I also wrote a similar tool long ago when I needed to recursively score positions for the tic-tac-toe solver, using a retrograde analysis similar to what's used to generate endgame tablebases for chess. (It literally took hours with the plain old "search array"!).
(A similar tool is the "search ordered table.vi", which only works for DBL and returns a fractional index.)
Suggestion:
"Search array" should have a boolean input (default=FALSE) where we can tell the algorithm that the array is sorted.
(The output would be exactly as before, with -1 indicating "not found".). Setting this input to TRUE would switch to a binary search where in the worst case only ln2(N) instead of N comparisons need to be made. (e.g. 20 instead of 1048576 for a 50000x speedup!!!)
It could look as follows.
Of course it should continue to accept all possible datatypes (including clusters) and not be limited to simple datatypes as the polymorphic example quoted above.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.