Not all algorithmic thinking occurs in front of a monitor with a keyboard at hand. In fact, it is often the case that the more complex an algorithm is, the more benefit there is in postponing writing its implementation in actual code, and to reason about it in different ways. One tool for doing this this the Pseudocode. There is also sometimes a need to communicate the essence of an algorithm to others who may not know the same programming language as you, in which case pseudocode is also very useful.

Pseudocode is a way of describing algorithms which is

  • Simple
  • Clear
  • Unambiguous
  • Language-agnostic

Since Python came along as a programming language, the need for pseudocode has decreased somewhat, as Python already meets the first 3 of these criteria! As for the fourth, Python syntax is so clear that the intent of an algorithm written using it is generally obvious.

In practice people often use a kind of Python-lite as pseudocode. This avoids some of the more advanced features and idiosyncrasies of Python and communicates algorithms in a way which is easily understood by programmers from other backgrounds.

Aside from the Python-lite version of pseudocode there are other styles which it is useful to be familiar with. Some are quite close to Python-lite, and some more akin to C-style languages.

Look at these two examples:

Selection Sort Pseudocode

Python-Lite-Style Pseudocode

Selection sort pseudocode

(From Introduction to the design & analysis of algorithms / Anany Levitin.)

  • The comment symbol is different: // instead of #
  • The word do replaces the colon in Python
  • The use of indentation for grouping commands is the same as with Python
  • SelectionSort(A[0..,n−1]) is the function header, showing that the input argument is an array whose indices range from 0 to n-1.

As an Amazon Associate I earn from qualifying purchases.

C-Style Pseudocode

C-Style Pseudocode

(From My Code School.)

  • Braces ({}) are used to delineate blocks of code
  • Backwards arrow (<-) is used for assignment
  • The semicolons you might expect in c-style languages have been omitted

Although you may be more familiar with one style than the other, both these examples do the job of communicating clearly and unambiguously what needs to happen for selection sort to work.

The specific use of pseudocode in UK Computer Science syllabuses is a topic where I have strong opinions, which you can read about here if you are interested.

This post has discussed the use of pseudocode as a way to communicate and reason about algorithms.

Happy Computing!

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *