Implimenting search algorithms


Gammatech

Recommended Posts

I'm not sure whether this should be asked here or under channels, but here goes.

I have a channel that has the result from a batch process. As the user has control of the batch constraints the times for each entry in the channel may be equally spaced (if the batch was set for a fixed time) or not (if the batch was set for fixed quantities) The pattern of the time stamps may even change in the channel if the user changed the constraint for the batch.

I want to find the Channel entry that best matches a time given by the user.

As the channel has at most 600 entries (History and persistence) but could possibly be empty if the user is not monitoring in batches or the first batch has not yet finished I am not sure of the best search method to employ.

The user entered target time will be only to a resolution of 1 hour so the search would be for the first match for Floor(My_Channel.Time/3600)*3600

The Binary Search algorithm would need to return more than one value (Top & Bottom of the Channel portion and a flag for whether the time has been found) if it is to be used recursively. It would seem that a sequence used as a function can only return one value so would global variables need to be used to return the other values?(top & bottom)

Any suggestions gratefully received.

Martin

Link to comment
Share on other sites

First, coding up a binary search for only 600 entries is a waste, especially when you can use the search() function which will search 10,000 entries faster than you could possibly search 600 in script (because its in C). Search() actually searches linearly (since it doesn't know of any indexing), which actually works to your advantage. You can just do search(x.time < target) since it will go from [0] (biggest time) to smallest. Search() will return the index into the array, or -1 for not found.

As for returning multiple values, globals work but are messy. You could create a class to act as a structure and return that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.