Shortcuts binary search algorithm

Has anyone attempted to create a shortcuts binary search algorithm? I’ve been kicking it around but it’s just not working.

Here is my test Shortcut.
https://www.icloud.com/shortcuts/53c2a0940acc4a3da3a48b39f507ff15

I’ve not attempted to create one, until you posted this, but maybe something like this?

I’ve tried to use some more explicitly named variables, some comments and even a bit of extra code for debugging to try and help anyone reading it make sense of it. As a result of all of the extra cruft, I’m sure it is running slower.

If you wanted to speed things up, it might make sense to look at a recursive shortcut that splits the list or simply keeps passing it along and effectively turn it into a function / parameter based subroutine.

Alternatively, I’m pretty sure that if your data set was large enough to warrant such a search for performance reasons, that it would further be worth applying JavaScript to the approach as it could then take advantage of browser optimisations. I posted a solution to something else recently that takes exactly that sort of thing to consideration…

Hope that helps.

1 Like
  1. You’re amazing!
  2. It totally made my Shortcut run faster. I have 5000 records that I need to find just one instance of. In my original Shortcut using linear search it took just over 1 minute to find the record I was looking for. Tying into your Shortcut I found my record in under 15 seconds! Much better.
  3. I’m going to try your idea about JavaScript and see if I can’t optimize it even more. But it’s amazing that Shortcuts was able to handle the task at good speed. You were right about giving the variables more descriptive names as it can become very challenging to figure where you are in the function.

Thanks again Sylumer, You really showed my that Shortcuts can be used in ways I never thought possible.

1 Like