WhCode

What is an Algorithm?

➤ An algorithm is a step-by-step procedure or set of rules designed to perform a specific task or solve a particular problem. It is a systematic method that takes an input, processes it, and produces an output.

➤ Algorithms are generally created independent of underlying languages, i.e., an algorithm can be implemented in more than one programming language.


Categories of Algorithms (Data Structure Point of View)

From the data structure point of view, the 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 an 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.

Algorithms and Programming Languages

1. Language Independence

➤ Algorithms are abstract concepts that describe a series of steps to solve a problem. They are not tied to any specific programming language.

➤ This means the same algorithm can be expressed in multiple programming languages, such as Python, Java, C++, etc.

2. Implementation

➤ While the logic and structure of the algorithm remain the same, the syntax and specific functions may vary from one language to another.

➤ For example, a sorting algorithm like Bubble Sort can be implemented in various languages with different syntax but the same underlying logic.


Example: Bubble Sort Algorithm

Here’s how the Bubble Sort algorithm can be implemented in different programming languages:

Python

    
    def bubble_sort(arr):
        n = len(arr)
        for i in range(n):
            for j in range(0, n-i-1):
                if arr[j] > arr[j+1]:
                    arr[j], arr[j+1] = arr[j+1], arr[j]
        return arr
    
        

Java

    
    public class BubbleSort {
        public static int[] bubbleSort(int[] arr) {
            int n = arr.length;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n-i-1; j++) {
                    if (arr[j] > arr[j+1]) {
                        int temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
            return arr;
        }
    }
    
        

Characteristics of an Algorithm with Examples:

  1. Finiteness:
    • Definition: An algorithm must terminate after a finite number of steps.
    • Example: In the Bubble Sort Algorithm, the algorithm will eventually finish sorting the array after a specific number of comparisons and swaps, ensuring it does not run indefinitely.
  2. Definiteness:
    • Definition: Each step of the algorithm must be clear and unambiguous.
    • Example: In the Binary Search Algorithm, the instruction to "compare the middle element with the target value" is precise and clear, leaving no room for interpretation.
  3. Input:
    • Definition: An algorithm can have zero or more inputs.
    • Example: In the Factorial Calculation Algorithm, the input is a non-negative integer n, for which the factorial (n!) is to be calculated. For instance, if n = 5, the input is 5.
  4. Output:
    • Definition: An algorithm produces one or more outputs.
    • Example: In the Fibonacci Sequence Algorithm, if the input is n = 5, the output will be the fifth Fibonacci number, which is 5.
  5. Effectiveness:
    • Definition: All operations in the algorithm should be basic enough to be performed by a person using pen and paper.
    • Example: In the Addition Algorithm, adding two numbers (e.g., 3 + 5) involves simple arithmetic that anyone can perform manually.