Hi everyoneπ,
From the last post, Programming Concept - #Algorithm, I discussed what algorithm is and also gave an example. Now this post is not about Data Structure and Algorithm, yet!. The idea behind this series is to take you from the beginner level to advanced level in programming so make sure you stay tuned π».
Pseudo code as mentioned early is basically a false code. Computers understand only two vocabularies i.e. 0 and 1. For every programming language, there is a syntactic rule to follow but with pseudo code, you can write the code in your own language and that is the beauty of it.
Algorithm is all about performing a task and before doing any task, you must first understand what the task is. Every task has it's own requirements and there can be multiple ways to perform the same task.
Let's take look at an example;
The bus driver has this map to pick up the school kids from their home labeled A, B, C. Can you guess the shortest route to use from point A, B and C?
- School -> A -> B -> C -> School
- School -> C -> A -> B -> School
- School -> B -> A -> C -> School
Above are 3 possible options to visit all points. Can you pick out the "best" option? Of course, it will be the second option. Why? Because the driver might consider the using less fuel and shortest time. Though we have multiple options, we will always consider the most efficient one. Efficiency is one of the key characteristics of a good algorithm.
Let's write down the pseudo code for the first option:
- Start the bus π
- Move from School ππ«
- Go to Point A π π
- Move from Point A ππ
- Go to Point Bπ‘π
- Move from Point B ππ‘
- Go to Point C ππ
- Move from Point C ππ
- Go to School π«π
- Stop bus engine π
....and that's it. See how simple you can write down the pseudo code? And this can be used as a blueprint to write code in different programming language.
It's always good to write down the steps when solving a problem in the form of a pseudo code. You can then further break it down into smaller modules making your program easy to manage.
Key characteristics to note about pseudo code:
- All actions for each step must be doable.
- Each step must be precisely define with no ambiguity
- Each step of the algorithm must be finite i.e. it must have an end
Note that your algorithm must also have a starting point and the end point just as every program cannot run infinitely.
In the next article, I will discuss how to write the algorithm graphically using flow chart.
Have a great day π and see you on the next one.
Top comments (0)