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
Let’s see how you can benefit from pseudocode before seeing a concrete example with a bubble sort algorithm.
There are many benefits to using pseudocode:
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
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 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).
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:
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!
Below are some keywords to give you inspiration.
Pseudocode is a plain
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
.