In this tutorial, we will learn about basic algorithms.
Before learning any programming language you should learn about basic algorithms which improve your logical thinking and problem-solving skills.
First,
What is an algorithm?
- It is an effective procedure for solving a problem in a finite number of steps.
- It is effective, which means that an answer is found and it has a finite number of steps.
- A well-designed algorithm will always provide an answer; it may not be the desired answer but there will be an answer.
- A well-designed algorithm is also guaranteed to terminate.
For writing any algorithm, the following has to be known :
1.Input
2.Task to be performed
3.Expected output
For example:
Suppose you check your result on any website,
- So first you enter your name or id number on that website.
- Website search your name or id number on their database.
- And you get your rank or marks.
In this case,
Input - Your Name/id number
The task to be performed - Search for your name/id number
Expected output - Your rank
The key features of an algorithm:-
- There are three key features in an algorithm.
1.Sequence (Process)
2.Decision (Selection)
3.Repetition (Iteration / Looping)
1.Sequence (Process) :
- It means that each step or process in the algorithm is executed in the specified order.
- Each process must be in the proper place otherwise the algorithm will fail.
2.Decision (Selection) - If...then, If...then...else.. :
- In the algorithm the outcome of a decision is either true or false, there is no state in between.
- The outcome of the decision is based on some conditions that can only result in true or false value.
For example :
If today is Sunday then holiday.
- The general form of decision (if...then...),
If proposition then process
It means, if the proposition is true then execute the process.
- A proposition is a statement, which can only true or false.
- The general form of decision (if...then...else..),
If proposition then
process1
else
process2
It means, If the proposition is true then execute process1 else or otherwise execute process2.
3.Repetition (Iteration / Looping)
- Repetition can be implemented using a construct like,
i-Repeat loop
ii-While loop
iii-If..then..goto.. loop
i-Repeat loop
- The repeat loop is used to iterate or repeat a process or sequence until some condition becomes false.
- The general form of the repeat loop is,
Repeat
process1
process2
.
.
.
processN
Until proposition
For example :
Repeat
fill water in the bottle
Until the bottle is not full
In the above example,
- If the bottle is already full at the start of the repeat loop, then filling more water will lead to an overflow.
- This is the drawback of a repeat loop.
ii-While loop
- The general form of while loop is,
while proposition
begin
process1
process2
.
.
.
processN
end
- In such a case the while loop is more appropriate. The above example with the while loop is shown as follows:
while the bottle is not full
fill water in the bottle
- Since the decision about the bottle is full or not is made before filling water, the possibility of an overflow is eliminated.
- The while loop finds out whether some condition is true before repeating a process or a sequence of processes.
- If the condition is false, the process or the sequence of processes is not executed.
iii-If..then..goto.. loop
- The if .. then goto .. is also used to repeat a process or a sequence of processes until the given proposition is false.
- In the bottle example, this construct would be implemented as follows:
Fill some water in the bottle
if the bottle not full then goto 1
- So as long as the proposition ‘bottle not full’ is true the process, ‘fill some water in a bottle’ is repeated.
- The general form of if .. then goto .. is:
Process1
Process2
.
.
ProcessN
if proposition then goto Process1.
0 Comments