A worked example of how to write an algorithm

As head librarian, so that students can browse more efficiently, I want to arrange the library so that books on similar topics are near to each other. I need to automate the process.

1. Consider a simple case of the problem and solve it yourself, either mentally or on paper.

2. Watch how your own brain goes about solving the problem. You may notice that there are a few competing ways of doing it; choose just one for the moment.

3. Write down the steps that your brain used.

4. Attempt to directly translate each written step into code or pseudocode. If the step can’t be so translated, break it down further by re-applying 2–4 on that written step. Because of the recursion, prefer a breadth-first translation process over a depth-first translation — you might notice gaps earlier with a breadth-first pass if a depth-first pass goes very deep.

5. If the resulting algorithm is likely to be efficient enough for your purposes, stop and have a cup of tea. If efficiency might become a concern at some point return to step 1 or 2 and find an alternative algorithm.



This is my programming blog. www.github.com/david-mears

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store