There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of some important functions which are present in a workbook called the library.

There are various types of functions present in the C++ Algorithms Library. Let us straightway look at the categories.

Min/Max:

Merge:

Sorting:

The above functions are immensely used while programming. Since there are hundreds of such functions present in the STL library, we’ll be discussing some of them using a coding example.

Simplest sorting algorithm: Bubble Sort

Code:

Output:

Explanation:

The above code is a simple depiction of a sorting technique popularly known as Bubble Sort. Bubble sort works on the following algorithm:

The first loop is started from the 0th element and the next loop is iterated from an adjacent element. In the inner loop, we compare each associated element and swap them if they are found not in order. Additionally, at the end of each iteration performed by the outer loop, the heaviest or the greatest element is bubbled up at the end.

The comparison is done using passes. Passes are nothing but the swapped value that occurs while iterating through each element. In this case, 3 passes are occurring to sort the given items. With each pass the largest element is shifted last i.e. after the first pass, the largest element is shifted last. In second pass, the largest item is shifted at 2nd last position, and so on.

Min/Max Algorithm

Minmax function example:

#include<bits/stdc++.h>

using namespace std;

int main ()

{

auto values = minmax({100,222,324,401,500});

cout << “Minimum and Maximum values are: “;

cout << values.first << ‘ ‘ << values.second << endl;

return 0;

}

Output:

Explanation:

The above code is a simple depiction of the minmax function discussed earlier. It is present in the <algorithm> header file thus using <bits/stdc++> imports all the necessary files required. In the above code, we have defined a variable ‘value’ which stores all the elements given. These elements are passed as an argument to minmax function and the result is displayed on the console in the form of a minimum and maximum values.

Searching Algorithm:

Binary Search:

Output:

Explanation:

Binary search works on the concept of following algorithm:

The binary search algorithm is typically used to find an element present in a huge data-set. It can be compared to a search of a contact in the telephone directory where the directory is divided into two equal parts and the range in which the proximity of getting element is maximum, the range yet again divided to reduce time in traversing through each element present.

Share This