What's a Pseudocode?

When working on a software solution (algorithm, apps…) with teammates, you need a way to quickly express your thoughts and ideas. This is where pseudocode comes to the rescue. As you can guess, pseudocode is a kind of code that is free from language-specific syntax (PHP, Python, JS…). In other words, pseudocode is simply the description of your algorithm in plain Englishi.e., French, Chinese, Lingala, etc.. It is only intended for humans to read.

Let’s see how you can benefit from pseudocode before seeing a concrete example with a bubble sort algorithm.

Benefits

There are many benefits to using pseudocode:

  1. Helps to reinforce your understanding of a solution before you implement it
  2. Easy to share and explain, as it is written in human language and not code
  3. No constraints: you can use any format you want
  4. Programming-free language: a solution of an algorithm written in pseudocode can be understood by all developers, even if they don’t use the same programming language.
  5. And more!

Implementation

Now is the time for hands-on practice (but this is the section you were actively looking for anyway, isn’t it?)

The purpose here is to write pseudocode for the bubble sort algorithma comparison-based sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong ascending order.

function bubbleSort(arr)
Set isSwapped to true
WHILE isSwapped = true
    Reset isSwapped to false
    FOR each item in the arr
        IF current item > next item
            swap items
            Reset isSwapped to true
        ENDIF
    ENDFOR
ENDWHILE
RETURN arr

As you can see, I just explained my logic flow so that you can easily implement it in your preferred language.

I have used the function keyword here to let you know that it is a function, though other people may use the procedure keyword. Every time I use a blocfor, if, etc., I make sure I use endBloc at the end (they are called scope terminators), and I capitalize them.

I also use indentation as much as I can, and I suggest you do so as well (for readability).

Better to know

The main rule for pseudocode is that there are no rules at all. Any rules in place will can depend on the school or company you are writing for.

Just make sure:

  • your logic flow is clear and easy enough to be understood by anyone
  • you’re consistent on the format you choose to use

Don’t be astonished if you see pseudocode following the format of a given language. > For example, a C developer may naturally format his pseudocode following the C coding style.

The main thing is that your pseudocode must be easy to consume by almost everyone without much effort; however, this should not keep you from getting creative, if you want!

Some keywords to use

Below are some keywords to give you inspiration.

Pseudocode Key-words

Closing notes

Pseudocode is a plain Englishor other language written process of how you can proceed to resolve an algorithm or any given computer problem.

There’s no formal rule for pseudo-coding, rather it depends on your personal style, school, or company. The most important thing is consistency in your chosen style. You can make use of keywords such as Do While...EndDo, If...Endif, and Return.