Classification of Algorithms with Examples


Classification of algorithms helps in selecting the most suitable one for a specific task, enabling developers to optimize their code and achieve better performance. In computer science, algorithms are sets of well-defined instructions used to solve problems or perform specific tasks. The efficiency and effectiveness of these algorithms are crucial in determining the overall performance of a program.

In this article, we will discuss two common approaches for classifying algorithms, namely, based on their time complexity and based on their design technique.

Syntax

The syntax of the main function used in the codes for both approaches −

int main() {
   // Your code here
}

Algorithm

  • Identify the problem to be solved.

  • Choose the appropriate approach for classifying algorithms.

  • Write the code using the chosen approach in C++.

  • Compile and run the code.

  • Analyze the output.

What is Time Complexity?

Time complexity is a measure of the amount of time an algorithm takes to run as a function of the size of the input. It's a way to describe how efficient an algorithm is and how well it scales with larger inputs.

The time complexity is usually expressed using big O notation, which gives an upper bound on the running time of an algorithm. For example, an algorithm with a time complexity of O(1) means that the running time stays constant regardless of the size of the input, while an algorithm with a time complexity of O(n^2) means that the running time grows quadratically with the size of the input. Understanding the time complexity of an algorithm is important when choosing the right algorithm for a problem and when comparing different algorithms.

Approach 1: Classifying algorithms based on their time complexity

This approach encompasses categorizing algorithms in accordance with their elapsed time intricacy.

This entails deciphering the duration intricacy of an algorithm first, and then categorizing it among the five classifications based on its elapsed time intricacy: O(1) constant time intricacy, O(log n) logarithmic time intricacy, O(n) linear time intricacy, O(n^2) quadratic time intricacy, or O(2^n) exponential time intricacy. This categorization sheds light on the effectiveness of an algorithm, and the choice of algorithm can be made with due consideration to the size of the input data and the desired completion time.

Example-1

The following code showcases a demonstration of the linear search algorithm, boasting a linear time complexity of O(n). The algorithm conducts a systematic inspection of the elements within an array, determining if any of them match the specified search element. Upon discovery, the function returns the index of the element, otherwise it returns a value of -1 indicating the element's absence from the array. The main function sets into motion by initializing the array and search element, invoking the linearSearch function, and finally presenting the outcome.

#include <iostream>
#include <vector>
#include <algorithm>
// Linear search function with linear time complexity O(n)
int linearSearch(const std::vector<int>& arr, int x) {
    for (size_t i = 0; i < arr.size(); i++) {
        if (arr[i] == x) {
            return static_cast<int>(i);
        }
    }
    return -1;
}
int main() {
    std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int search_element = 5;
    int result = linearSearch(arr, search_element);
    if (result != -1) {
        std::cout << "Element found at index: " << result << std::endl;
    } else {
        std::cout << "Element not found in the array." << std::endl;
    }
    return 0;
}

Output

Element found at index: 4

Approach 2: Classifying algorithms based on their design technique.

  • Analyze the design technique of the algorithm.

  • Classify the algorithm into one of the following categories−

    • Brute-force algorithm

    • Divide and conquer algorithm

    • Greedy algorithm

    • Dynamic programming algorithm

    • Backtracking algorithm

Example-2

The below program exhibits the implementation of the binary search algorithm, which leverages the divide and conquer strategy and boasts a logarithmic time complexity of O(log n). The algorithm repeatedly bisects the array into two sections and examines the central element. If this middle element is congruent with the sought-after search element, the index is promptly returned. If the middle element surpasses the search element, the search persists within the left half of the array, alternatively, within the right half if the middle element is lesser. The main function initiates by initializing the array and search element, arranging the array through sorting, invoking the binarySearch function, and finally presenting the result.

#include <iostream>
#include <vector>
#include <algorithm>

// Binary search function using divide and conquer technique with logarithmic time complexity O(log n)
int binarySearch(const std::vector<int>& arr, int left, int right, int x) {
   if (right >= left) {
      int mid = left + (right - left) / 2;

      if (arr[mid] == x) {
         return mid;
      }

      if (arr[mid] > x) {
         return binarySearch(arr, left, mid - 1, x);
      }

      return binarySearch(arr, mid + 1, right, x);
   }
   return -1;
}

int main() {
   std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
   int search_element = 5;

   // The binary search algorithm assumes that the array is sorted.
   std::sort(arr.begin(), arr.end());

   int result = binarySearch(arr, 0, static_cast<int>(arr.size()) - 1, search_element);

   if (result != -1) {
      std::cout << "Element found at index: " << result <<std::endl;
   } else {
      std::cout << "Element not found in the array." << std::endl;
   }
   return 0;
}

Output

Element found at index: 4

Conclusion

Thus, in this article, two methods are discussed for categorizing algorithms - based on their time complexity and based on their design approach. As exemplars, we presented a linear search algorithm and a binary search algorithm, both executed in C++. The linear search algorithm, which adopts a brute-force method, boasts a linear time complexity of O(n), whereas the binary search algorithm, utilizing the divide and conquer approach, exhibits a logarithmic time complexity of O(log n). A comprehensive comprehension of the various classifications of algorithms will assist in selecting the optimal algorithm for a specific task and refining the code for improved performance.

Updated on: 21-Jul-2023

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements