Algorithms with Examples

An algorithm is a step-by-step procedure or a set of rules to solve a problem or complete a task. Algorithms are essential in computer programming and can be implemented in various programming languages. They are the foundation of problem-solving in computer science and software development. Below, we will explore different types of algorithms along with examples to help you understand their workings.


1. Linear Search Algorithm

Purpose:
Linear Search is a simple algorithm used to find a particular element in a list or array. It works by checking each element of the list sequentially until the desired element is found.

Steps:

  1. Start from the first element of the array.
  2. Compare the target value with the current element.
  3. If the element matches the target, return the index.
  4. If the element does not match, move to the next element.
  5. Repeat steps 2 and 3 until the element is found or the end of the array is reached.

Example:

Let’s say we have the following list and we want to search for the number 8.

List: [1, 4, 7, 8, 2, 9]

Linear Search Algorithm:

  1. Start at index 0 (element 1). Not a match.
  2. Move to index 1 (element 4). Not a match.
  3. Move to index 2 (element 7). Not a match.
  4. Move to index 3 (element 8). Match found!

The algorithm will return the index 3 where the element 8 is located.


2. Binary Search Algorithm

Purpose:
Binary Search is a more efficient algorithm for finding a target element in a sorted list. It works by repeatedly dividing the search interval in half.

Steps:

  1. Begin with the middle element of the sorted list.
  2. If the middle element is the target, return its index.
  3. If the target is less than the middle element, repeat the search in the left half.
  4. If the target is greater than the middle element, repeat the search in the right half.
  5. Repeat steps 2-4 until the element is found or the interval is empty.

Example:

List: [1, 4, 7, 8, 9, 11, 13]
Target: 8

Binary Search Algorithm:

  1. The middle element is at index 3 (element 8).
  2. Since the middle element is equal to the target, return index 3.

The algorithm will return 3, where the element 8 is located.


3. Bubble Sort Algorithm

Purpose:
Bubble Sort is a simple algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The process is repeated until the list is sorted.

Steps:

  1. Start at the beginning of the list.
  2. Compare each pair of adjacent elements.
  3. If the elements are in the wrong order, swap them.
  4. After one pass, the largest element will be at the end of the list.
  5. Repeat the process for the remaining unsorted elements.

Example:

List: [5, 3, 8, 4, 2]

Bubble Sort Algorithm:

  1. Compare 5 and 3. Since 5 > 3, swap them. List becomes: [3, 5, 8, 4, 2]
  2. Compare 5 and 8. No swap needed.
  3. Compare 8 and 4. Since 8 > 4, swap them. List becomes: [3, 5, 4, 8, 2]
  4. Compare 8 and 2. Since 8 > 2, swap them. List becomes: [3, 5, 4, 2, 8] (now 8 is at the correct position).
  5. Repeat the process for the remaining elements. After the next pass, the list becomes: [3, 4, 2, 5, 8].
  6. After the next pass, the list becomes: [2, 3, 4, 5, 8].

The sorted list is: [2, 3, 4, 5, 8].


4. Insertion Sort Algorithm

Purpose:
Insertion Sort is an algorithm that builds the sorted array one element at a time. It takes each element from the unsorted part of the list and inserts it into its correct position within the sorted part.

Steps:

  1. Start with the second element of the list (the first element is considered sorted).
  2. Compare the current element with the elements in the sorted part of the list.
  3. Shift the larger elements to the right to make space for the current element.
  4. Insert the current element into the correct position.
  5. Repeat until the entire list is sorted.

Example:

List: [5, 3, 8, 4, 2]

Insertion Sort Algorithm:

  1. Start with element 3 (second element). Compare with 5. Since 3 < 5, move 5 to the right. Insert 3 in the first position. List: [3, 5, 8, 4, 2]
  2. Move to element 8. It’s already in the correct position.
  3. Move to element 4. Compare with 8 and 5. Move 8 and 5 to the right, and insert 4. List: [3, 4, 5, 8, 2]
  4. Move to element 2. Compare with 8, 5, 4, and 3. Move all elements to the right and insert 2. List: [2, 3, 4, 5, 8]

The sorted list is: [2, 3, 4, 5, 8].


5. Factorial Algorithm (Recursive)

Purpose:
The factorial of a non-negative integer nn is the product of all positive integers less than or equal to nn. This algorithm is often implemented using recursion.

Formula:

n!=n×(n−1)×(n−2)×…×1n! = n \times (n – 1) \times (n – 2) \times \ldots \times 1

Where:

  • n!=1n! = 1 if n=0n = 0 or n=1n = 1.

Steps:

  1. Define the base case (factorial of 0 or 1 is 1).
  2. For any other positive integer nn, recursively multiply nn by the factorial of n−1n-1.

Example:

To calculate 5!5!:

Recursive Factorial Algorithm:

  1. 5!=5×4!5! = 5 \times 4!
  2. 4!=4×3!4! = 4 \times 3!
  3. 3!=3×2!3! = 3 \times 2!
  4. 2!=2×1!2! = 2 \times 1!
  5. 1!=11! = 1

So: 5!=5×4×3×2×1=1205! = 5 \times 4 \times 3 \times 2 \times 1 = 120


Conclusion

Algorithms are fundamental tools in computer science, providing a structured way to solve problems and perform tasks efficiently. The examples provided—Linear Search, Binary Search, Bubble Sort, Insertion Sort, and Factorial Algorithm—demonstrate various types of algorithms and their applications.

Each algorithm has its use cases and complexity, and understanding the differences between them is essential in selecting the right algorithm for a given problem. The study of algorithms also introduces concepts such as time complexity and space complexity, which are important for evaluating the efficiency of an algorithm.

Leave a Reply

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