[go: up one dir, main page]

0% found this document useful (0 votes)
2 views5 pages

Algorithm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Algorithm

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Notion of Algorithm :

Algorithm
An algorithm is a finite set of instructions to solve a problem. Algorithm is a step-by-step procedure
which are executed in a certain order to get the desired output. Algorithms tell the programmers
how to code the program. The Algorithm designed are language-independent, i.e. they are just plain
instructions that can be implemented in any language.

Fig : The notion of the algorithm

Algorithm for Adding Two Numbers

1. Declare two variables: num1 and num2.


2. Take input values for num1 and num2.
3. Compute the sum: sum = num1 + num2.
4. Display the result (sum).

Characteristics of an Algorithm / Criteria of algorithm

The performance of an algorithm depends on certain criteria or conditions. These criteria are also
called as characteristics of the algorithm. Every best algorithm follows these criteria. They are:

1. Input - An algorithm should have zero or more well-defined inputs.


Example:
A "Hello World" algorithm needs no input (just prints a message).
A "Sum of Two Numbers" algorithm needs two inputs (the numbers to add).
2. Output - It should produce at least one output.
Example:
A sorting algorithm takes a list as input and outputs the sorted list.

3. Finiteness: An algorithm must terminate after a finite number of steps. It should not end up
in an infinite loop.
Example:
A correct algorithm to find the largest number in a list will check each number once and then
stop. An incorrect version might keep running forever without giving an answer.

4. Definiteness : The algorithm should be clear and unambiguous. The instruction used in the
designed algorithm must be definite without leading to multiple meanings.
Wrong Example (Not Definite)
Problem: Find the square of a number.
1. Take a number .
2. Do some math operations.
3. Return the result.
Why it is wrong?
 Step 2 says "Do some math operations" → What operations? Multiply? Add? Unclear!
 A programmer could interpret this in multiple ways.

Good Example (Definite)


Problem: Find the square of a number.
1. INPUT number
2. square ← number * number
3. OUTPUT square

Why it works?
 Step 1: Clearly specifies input.
 Step 2: Exact operation (number * number).
 Step 3: Clearly outputs the result.

5. Effectiveness - Each step or instruction used in the designed algorithm must be effective so
that it can be solved easily.

Key Rules for Effectiveness -


1. Avoid impossible steps
2. Use basic operations
3. Don’t overcomplicate

Explanation of all characteristics using an example -


Algorithm: Find Maximum Number in a List
1. Input
A list of numbers (e.g., [3, 7, 2, 9, 4]).
2. Output
The largest number in the list (e.g., 9).
If the list is empty, output an error or None.
3. Finiteness
The algorithm loops through each number exactly once and stops.
No infinite loops!
4. Definiteness
Each step is unambiguous:
Step 1: Start with the first number as the temporary max.
Step 2: Compare each subsequent number to the temporary max.
Step 3: Update the max if a larger number is found.
5. Effectiveness
Comparisons (>) and assignments (max = number) are basic operations.
No impossible steps (e.g., no division by zero).
Algorithm V/s Program
Aspect Algorithm Program
A step-by-step procedure to An implementation of an algorithm
1. Definition
solve a problem. using a programming language.
2. Meaning Design (logical plan) Implementation (actual code)
3. Nature Abstract. Concrete.
Independent of Hardware and Dependent on Hardware and
4. Dependency
software. Software.
Written in programming languages (C+
5. Representation Natural / Normal language
+, Python, etc.).
Focuses on logic and problem- Focuses on execution and
6. Purpose
solving. functionality.
7. Stage Analyze Test (Errors)

Abstract = Idea | Concrete = Reality


Que. Is there any difference between algorithm, pseudocode and program ?
1. Algorithm

 Definition: A step-by-step procedure or set of rules designed to solve a problem or perform a


task.
 Language: Not written in any programming language.
 Purpose: Focuses on logic, not syntax.
Example:
Step 1: Start
Step 2: Input two numbers
Step 3: Add the numbers
Step 4: Display the result
Step 5: Stop

2. Pseudocode
 Definition: A high-level representation of an algorithm that looks like code but is written in
plain English language. In short, a pseudocode is a mixture of a natural language and
programming language. A pseudocode is usually more precise than natural language.
 Language: Not an actual programming language. Just a way to describe logic clearly.
 Purpose: Bridges the gap between an algorithm and a real program.
Example:
START
Read A, B
Sum ← A + B
Print Sum
END

3. Program
 Definition: A coded version of an algorithm written in a specific programming language like
Java, C++, Python, etc.
 Language: Must follow the syntax and rules of that language.
 Purpose: It’s the final form that can be executed by a computer.

Example (in Java):

import java.util.Scanner;
public class AddNumbers
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int B = sc.nextInt();
int Sum = A + B;
System.out.println("Sum = " + Sum);
}
}

Summary Table

Aspect Algorithm Pseudocode Program


Language Plain English/logical steps Informal code-like English Specific programming language
Focus Logic & Steps Structure & Readability Syntax + Execution
Executable No No Yes
Purpose Solve a problem logically Represent logic clearly Get real output

You might also like