Fun with Flowcode - Binary Search

Use this section to discuss your embedded Flowcode projects.
Post Reply
mnfisher
Valued Contributor
Posts: 1931
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 156 times
Been thanked: 910 times

Flowcode v11 Fun with Flowcode - Binary Search

Post by mnfisher »

One common task for computers is searching data and retrieving values. Search is important - and returning a value often needs to be fast - equally determining that a value doesn't exist must be efficient too!

One useful algorithm for this purpose - is the binary search. This requires that the data is sorted - and here I use a simple insertion sort - and works by splitting the data into 2 halves - and comparing the search string to the middle point - deciding whether to search the 'upper' or 'lower' half. I use recursion - although an iterative algorithm is also possible.

This (very contrived) example - creates 5000 10 character strings. It then searches for each of these (- it 'knows' the location - so really the search is superfluous) - and should (does) find all of them. Then it searches for 5000 random strings - the probability is that none will be found.
Originally I stored a 'value' for each string - but to minimise the code I removed this - and BinarySearch just returns the location of the string (or -1) - this could be used to index am array of values if needed.

I missed a trick - the binary search could be modified slightly to find the insertion point for new data.
Typically - adding data is a less frequent (or one time) operation - and may be slower. I had to add a small delay to Generate to avoid WDT issues on esp32.

It is a MUCH quicker search algorithm than a linear search - which is left as an exercise :-)

Note that it works in simulation but DOESN'T on esp32 - due to a bug in CAL_STRING.c. I patched this and it does work (satisfyingly quickly) on hardware.

Collectors of interesting error messages - I originally tried an array of 10000 - and I got, rather unexpectedly' - 'invalid initialiser' - for i in Generate ( = (0); ) - a memory issue for sure?

Martin
Attachments
binary_search.fcfx
(23.42 KiB) Downloaded 22 times

chipfryer27
Valued Contributor
Posts: 1965
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 430 times
Been thanked: 647 times

Re: Fun with Flowcode - Binary Search

Post by chipfryer27 »

Martin

I know where you live (in a non threatening way) and I really think you need to get outside and enjoy it more...... :lol: :lol: :lol:

Regards
(As always in admiration)

mnfisher
Valued Contributor
Posts: 1931
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 156 times
Been thanked: 910 times

Re: Fun with Flowcode - Binary Search

Post by mnfisher »

Possibly.... Did have a look out for the Lyrids meteor shower this evening without success (peak is still a day or two away)

It was cold outside... No kind of atmosphere..

All together now 🤗

chipfryer27
Valued Contributor
Posts: 1965
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 430 times
Been thanked: 647 times

Re: Fun with Flowcode - Binary Search

Post by chipfryer27 »

Permission to engage laughing circuits....

Post Reply