An algorithm is step by step procedure to solve a particular problem. An algorithm provides a blueprint for writing a program to solve a particular problem. Once we have an idea or a blueprint of a solution, we can implement it in any language, such as C, C++, Java, C# etc.
Characteristics of an Algorithm :
Inputs: An algorithm has zero or more but only finite number of inputs.
Output: An algorithm has one or more outputs. The requirement of at least one output is obviously essential otherwise we can not know the answer/solution provided by the algorithm.
Finiteness: An algorithm must terminate after a finite number of steps and further each step must be executable in finite amount of time.
Definiteness : Each step of an algorithm must be precisely defined; the action to be carried out must be rigorously and unambiguously specified for each case.
Effectiveness: An algorithm should be effective.
Example : Write an algorithm for interchanging/swapping two values.
Step 1: Input first number as A
Step 2: Input second number as B
Step 3: Set temp = A
Step 4: Set A = B
Step 5: Set B = temp
Step 6: Print A, B
Step 7: End
Example : Write an algorithm to find the sum of first N natural numbers.
Step 1: Input N
Step 2: Set I = 1, sum 0
Step 3: Repeat Steps 4 and 5 while I <= N
Step 4: Set sum = sum + I
Step 5: Set I = I + 1
[END OF LOOP]
Step 6: Print sum
Step 7: End
0 Comments