Tuesday, March 7, 2017

Algorithms: What's the difference between an Algorithm and Psuedo-code?

This blog is based off of a question from one of my subscribers, Debojit Acharjee:
“Hi! I want to know how to write an algorithm and is there any syntax for writing algorithm? What is the difference between algorithm and pseudocode?”
Debojit, in order to understand the clear difference, how to write algorithms, and whether or not we have the syntax available to write those algorithms, we need to understand these terms first. If you look up “algorithm” in the Oxford dictionary, you will find the definition “a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer”.
This definition does not bind you to use a particular syntax when writing an algorithm. But, it does mention some phrases, like “rules to be followed”, “calculations”, “problem-solving operations”, and “especially by a computer”, which can’t be imagined without some formal discipline. Therefore, you don’t have to follow a specific strict grammar (syntax) to write an algorithm, per se, as long as you are able to communicate “the process” or “the set of rules” to do a particular task or solve a problem, and of course, doing it in a finite number of steps, optimally.
Next, let’s say you want to write an algorithm to arrange three distinct random numbers in ascending order. You can do so in plain English, using block diagrams, pseudocode, or you could create a flowchart. “Algorithm” is a term used mostly in Mathematics and Computing. Therefore, you write algorithms to communicate with other people in fields like Mathematics and Computing. You wouldn’t write an algorithm for preparing a cup of tea for your breakfast, but you would certainly write an algorithm to sort 10 random numbers in ascending or descending order.
“Algorithm” is a formal term. One example of an algorithm you could write is to find the biggest of three distinct integers, and here’s an example:
Example of a flowchart showing an algorithm to find the biggest of 3 distinct integers.

Let’s get back to the Oxford dictionary again: it defines
pseudo-code as “a notation resembling a simplified programming language, used in program design”.
Analyzing this word, you see it consists of two separate words, “pseudo”, and “code”. Let’s break this down:
The word “pseudo” loosely means “false”, or “not actual”. When read with the second word, it’s “pseudo-code”, which should lead you to its meaning - which is false (close to) code. Pseudo-code is the English-y representation of code, but it’s not actual code. It’s written as if you are actually writing code, but the programming language and hardware specifics have been taken away from it for two main reasons:
  1. Universally Understandable. One of the main goals of pseudo-code is to be understandable by all coders - universally.
  2. Universally Translatable. Once all coders are able to understand the pseudo-code, just like someone who knows 2 different languages and is able to translate between them, the coders can then implement the solutions that the pseudo-code is providing in their choice of programming language.
Pseudo-code to display the biggest of the three distinct integers would look like:
START
var: n1, n2, n3, BiggestInt
READ n1, n2, n3
BiggestInt = n1
IF BiggestInt < n2
   BiggestInt = n2
ENDIF
IF BiggestInt < n3
   BiggestInt = n3
ENDIF
DISPLAY “The biggest integer is:”, BiggestInt
END
I hope this helps, Debojit!

No comments:

Post a Comment