Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Programming. No Comments on Algorithm in Computer Science
Algorithm
5
(1)

This article provides a comprehensive overview of algorithm in computer science field along with relevant examples. If not already done, go through this program development steps article.

 

Definition

What is an algorithm?

An algorithm is a finite sequence of step by step, discrete, unambiguous instructions for solving a particular problem.


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Or

Algorithm is a step-by-step procedure for solving a particular problem.

Or

An algorithm is an ordered set of unambiguous executable steps, defining a terminating process.

 

An algorithm, named after 9th centaury Persian mathematician al-Khowarazmi, is simply a set of rules for carrying out some calculation, either by hand, or, more usually, on a machine.

 

 An algorithm is like a recipe. It lists the steps involved in accomplishing a task.  It can be defined in programming terms as a set of detailed, unambiguous and ordered instructions developed to describe the processes necessary to produce the desired output from the given input.

 

The algorithm is written in simple English and is not a formalized procedure, however to be useful there are some principles which should be adhered to. An algorithm must:

  1. Be precise and unambiguous
  2. Give the correct solution in all cases and
  3. Eventually end.

 

Algorithm representations

Algorithms can be represented in different ways. They are:

  • Natural languages
  • Pseudocode
  • Flowcharts
  • Control tables

 

Watch this video to learn about algorithms:

 

Pseudocode

 Pseudocode is easy to read and write, as it represents the statements of an algorithm in English.  Pseudocode is really structured English. It is English which has been formalised and abbreviated to look very like a high-level computer language.

 

There is currently no standard pseudocode. Different textbook authors seem to adopt their own special techniques and set of rules, which often resemble a particular programming language. But as a guideline, try the following conventions:

  • Statements are written in simple English
  • Each instruction is written on a separate line
  • Keywords and indentation are used to signify particular control structures
  • Groups of statements may be formed into modules and that group given a name

 

If a programmer uses words and phrases in his or her pseudocode that are in line with basic computer operations, then the translation from the pseudocode algorithm to a specific programming language becomes quite simple.

 

We could break down the computers operations into six basic operations and represent these operations in pseudocode. Each operation can be represented as an English instruction with keywords and indentation to signify a particular control structure:

 

  1. A computer can receive information – when a computer is required to receive information or input from a particular source, whether it is a keyboard, disk or other source, the verbs Read and Get can be used:
e.g.   
Read student name 
Get system date 
Read number_1, number_2

 

  1. A computer can put out information – when a computer is required to supply information or output to a device the verbs Print, Write or Put could be used:
e.g.
Print student number 
Write ’Program completed’ 
Write customer record to master file 
Put out name, address, postcode

 

  1. A computer can perform arithmetic – most programs require the computer to perform some sort of mathematical calculation, or formula. You could use actual mathematical symbols or the words for those symbols:
e.g.   
Add Number to Total 
Total = Total + Number 
Divide Totalmarks by Studentcount 
SalesTax = Costprice * 0.10 
Compute C = (F – 32) * 5/9

 

  1. A computer can assign a value to a piece of data – there are many cases where a programmer will need to assign a value to a piece of information. Firstly to give data an initial value in pseudocode the verbs, Initialise or Set can be used. Secondly to assign a value as a result of some processing, the symbol “=” is written. Thirdly, to keep a piece of information for later use the verbs Save or Store could be used
e.g.   
Initialise total accumulators to zero 
Set StudentCount to 0 
TotalPrice = CostPrice + SalesTax 
Store CustomerNumber in LastCustomerNumber

 

  1. A computer can compare two pieces of information and select one of two alternatives – an important ability is the comparison of two pieces of information and then as a result of the comparison select on of two alternate actions. To represent this operation in pseudocode the keywords IF, THEN and ELSE can be used
e.g.   
IF student is parttime THEN 
	Add 1 to parttimecount 
ELSE 
	Add 1 to fulltimecount 
ENDIF

 

Note the use of indentation to emphasize the THEN and ELSE options and the use of the delimiter ENDIF to close the operation.

 

  1. A computer can repeat a group of actions – when there is a sequence of steps which need to be repeated, the keywords DOWHILE and ENDDO could be used
e.g.  
DOWHILE studenttotal < 50 
	Read student record 
	Write student name, address to report 
	Add 1 to Studenttotal 
ENDDO

 

Once again note the use of indentation. It is easy to read where the loop starts and ends. Alternatives to the above could be WHILE and ENDWHILE.

 

Examples

Problem: Write an algorithm for adding two integers.

Algorithm for adding two integers

Algorithm: Add(x,y) 
To add two integer x and y
Inputs: x and y 
Output: Sum of x and y
 
Start 
1: Read two integers x and y 2
: Set sum = 0 
3: sum = x + y 
4: Display sum 
Stop

 

Problem: Write an algorithm to find the greatest number among two integers.

Algorithm for finding greatest of two numbers

Algorithm: Greatest(x,y)
To find the greatest among two integers

Inputs: x and y
Output: Greatest integer among x and y

Start
1: Read two integers x and y
2: IF x is greater than y THEN:
          2.1: Display “X is greater”
   ELSE
          2.1: Display “Y is greater”
   END IF
Stop

 

Next let’s learn about flowcharts.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *