Notes on Algorithm
What is an Algorithm?
An algorithm is a finite set of step-by-step instructions used to solve a problem or
perform a task.
Definition:
“An algorithm is a sequence of well-defined steps to solve a particular problem.”
Characteristics of an Algorithm
1. Input – Takes zero or more inputs.
2. Output – Produces at least one output.
3. Definiteness – Every step should be clear and unambiguous.
4. Finiteness – Must end after a finite number of steps.
5. Effectiveness – Steps should be simple and executable.
Advantages of Algorithm
1. Easy to understand.
2. Helps in solving complex problems step by step.
3. Independent of programming language.
4. Makes debugging easier.
5. Improves program efficiency.
6. Helps in planning before coding.
Disadvantages of Algorithm
1. Writing algorithms for large problems is time-consuming.
2. Complex algorithms may become difficult to understand.
3. Cannot show visual representation like flowcharts.
4. Detailed algorithms may take more space.
Steps to Write an Algorithm
1. Start the algorithm.
2. Take input from the user.
3. Process the input using logic/formulas.
4. Display the output.
5. Stop the algorithm.
General Format of an Algorithm
Step 1: Start
Step 2: Input values
Step 3: Process the data
Step 4: Display result
Step 5: Stop
1. Algorithm for Bubble Sort
Definition:
Bubble Sort repeatedly compares adjacent elements and swaps them if they are in
the wrong order.
Algorithm:
Step 1: Start
Step 2: Input array elements
Step 3: Repeat for i = 0 to n-1
Step 4: Repeat for j = 0 to n-i-1
Step 5: If arr[j] > arr[j+1], swap them
Step 6: End inner loop
Step 7: End outer loop
Step 8: Display sorted array
Step 9: Stop
Example:
Array: 5 3 8 1
After sorting: 1 3 5 8
2. Algorithm for Insertion Sort
Definition:
Insertion Sort places each element in its correct position in the sorted part of the
array.
Algorithm:
Step 1: Start
Step 2: Input array elements
Step 3: For i = 1 to n-1
Step 4: key = arr[i]
Step 5: j = i - 1
Step 6: While j >= 0 and arr[j] > key
Step 7: Shift elements and insert key
Step 8: Display sorted array
Step 9: Stop
Example:
Array: 9 5 1 4
After sorting: 1 4 5 9
3. Algorithm for Selection Sort
Definition:
Selection Sort repeatedly selects the smallest element and places it at the correct
position.
Algorithm:
Step 1: Start
Step 2: Input array elements
Step 3: For i = 0 to n-1
Step 4: Assume min = i
Step 5: Find smallest element
Step 6: Swap arr[i] and arr[min]
Step 7: Display sorted array
Step 8: Stop
Example:
Array: 7 2 9 1
After sorting: 1 2 7 9
4. Algorithm to Check Prime Number
Definition:
A prime number is divisible only by 1 and itself.
Algorithm:
Step 1: Start
Step 2: Input number n
Step 3: Set count = 0
Step 4: Check divisibility from 1 to n
Step 5: If count = 2, print Prime Number
Else print Not Prime Number
Step 6: Stop
Example:
Input: 7
Output: Prime Number
5. Algorithm to Check Palindrome Number
Definition:
A palindrome number remains the same when reversed.
Algorithm:
Step 1: Start
Step 2: Input number n
Step 3: Reverse the number
Step 4: Compare original and reversed number
Step 5: If equal, print Palindrome
Else print Not Palindrome
Step 6: Stop
Example:
Input: 121
Output: Palindrome Number
Difference Between Bubble, Insertion, and Selection Sort
Bubble Sort – Swaps adjacent elements
Insertion Sort – Inserts element at correct position
Selection Sort – Selects minimum element
Conclusion
Algorithms are important in programming because they provide a clear and
organized method to solve problems.