In this tutorial, we will learn about the Algorithm which is used to create a efficient or a readable programs in C language.
ALGORITHMS IN C LANGUAGE
What is Algorithm?
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. Algorithms are generally created independent of underlying languages, i.e. an algorithm can be implemented in more than one programming language.
Algorithm is always written in a simple English language.
Characteristics of an Algorithm
1. Unambiguous− Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning.
2. Fineness- An algorithm should terminate infinite number of steps and each step must finish in finite amount of time.
3. Input- An Algorithm should have 0 or more well-defined inputs.
4. Output- An algorithm should have 1 or more well-defined outputs, and should match the desired output
5.Independent− An algorithm should have step-by-step directions, which should be independent of any programming code.
From the data structure point of view, following are some important categories of algorithms −
- Search − Algorithm to search an item in a data structure.
- Sort − Algorithm to sort items in a certain order.
- Insert − Algorithm to insert item in a data structure.
- Update − Algorithm to update an existing item in a data structure.
- Delete − Algorithm to delete an existing item from a data structure.
Example
Let us try to learn algorithm-writing by using an example.
Problem − Design an algorithm to multiply two numbers and display the result.
Step 1 − START
Step 2 − declare three integers a, b & c
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP
Algorithms tell the programmers how to code the program. the algorithm can be written as −
Step 1 − START MULTIPLY
Step 2 − get values of a & b
Step 3 − c ← a * b
Step 4 − display c
Step 5 − STOP
0 Comments