Algorithms in C++ sort, find, reverse
The built-in Standard Template Library (STL) collection in the C++ programming language provides C++ users with a rich collection of multiple algorithms. These built-in supported algorithms are designed to implement multiple separate system tasks or operations on container data types such as vectors, lists, sets, etc., and other iterator methods. Existing STL algorithms in C++ work with the iterator data type, making them versatile across container data types for different uses and purposes. There are three main and most commonly used algorithm concepts in C++ programming: the sort, find, and reverse algorithms.

In C++, the sort, find, and reverse algorithms are built-in features or elements of the <algorithm> header file, which serves as a basic supported header file or directory for algorithms in C++ programming.
Sort Algorithm Concept in C++.
In C++ programming, the sort algorithm is used to arrange the stored data elements of container data types, such as vectors, arrays, etc., in a specific, unique order. Generally, you can arrange them in an increasing or decreasing order sequence.
Syntax of the Sort Algorithm.
#include <algorithm>
sort(begin_iterator, end_iterator);
Sort Algorithm Parameters.
- begin_iterator – This is an iterator method pointing to the first element of the current container data type element range.
- end_iterator – This is an iterator method pointing to the last element of the current container data type element range.
Behavior of the Default Sorting Algorithm.
By default, in C++ programming, you can arrange or set container data type elements in ascending order using the sort < operator.
Custom Sorting Methods.
In this C++ user can use a custom comparison function or lambda expression to modify the container data type sorting order sequence.
Example of the Sorting Algorithm’s usage.
#include <iostream>
#include <vector>
#include <algorithm> // Here we use the algorithm header file for the std::sort method
using namespace std;
int main() {
vector<int> vctr = {9, 13, 17, 5, 20, 30, 22};
// Here it sorts container data in ascending order. Default behaviour method:
sort(vctr.begin(), vctr.end());
cout << “Container data sorted in ascending sequence – “;
for (int data : vctr) {
cout << data << ” “;
}
cout << endl;
// Here it sorts container data elements in descending order using a custom comparator lambda method
sort(vctr.begin(), vctr.end(), greater<int>());
cout << “Container data sorted in descending sequence – “;
for (int data : vctr) {
cout << data << ” “;
}
cout << endl;
return 0;
}
Explanation of the Sorting Algorithm’s usage.
- In this example, the sort algorithm uses the greater<int>() comparison function to sort the vector vctr data elements by arranging them first in ascending order and then in descending order.
Find Algorithm Concept in C++.
In C++ programming, the find algorithm is used to find or search for an element-specific numeric value in a container data type. This C++ function returns an iterator value at the first occurrence of a container data element matching a user-supplied integer value.
Syntax of the Find Algorithm.
#include <algorithm>
auto it = find(begin_iterator, end_iterator, value);
Parameters of the Find Algorithm.
- begin_iterator – This is an iterator method for the first value element in the find data element range.
- end_iterator – This is an iterator method for the last element in the find data element range.
- value – This is the value in the find algorithm that you want to find in the container vector data type.
Find Algorithm returns value.
If the find element is found in the current container data type, it returns an iterator value indicating that container data element.
If the element is not found in the current data type, it returns the end_iterator value.
Example of Find Algorithm usage.
#include <iostream>
#include <vector>
#include <algorithm> // here we use algorithm header file For std::find method
using namespace std;
int main() {
vector<int> vctr = {21, 89, 64, 59, 11, 29, 01};
//here it searching for the value 67 in the vector data element
auto it = find(vctr.begin(), vctr.end(), 67);
if (it != vctr.end()) {
cout << “Search vector element found – ” << *it << endl;
} else {
cout << “Search vector element not found -” << endl;
}
// here it’s searching for a value that doesn’t exist (e.g., 11)
it = find(vctr.begin(), vctr.end(), 11);
if (it != vctr.end()) {
cout << “Search vector element found – ” << *it << endl;
} else {
cout << “Search vector element not found -” << endl;
}
return 0;
}
Explanation of Find Algorithm usage.
- In this example, the find function finds the numeric value 11 in the vector. Since it exists, it prints the statement “Search vector Element found – 11”.
- When searching for a 67 vector value that does not exist in the vector container data type, vctr.end() returns the value. Therefore, the program prints the statement “Search vector Element not found”.
Reverse Algorithm Concept in C++.
In C++ programming, the reverse algorithm is used to reverse the order of data elements stored in a container data type.
Syntax of the Reverse Algorithm.
#include <algorithm>
reverse(begin_iterator, end_iterator);
Parameters of the Reverse Algorithm.
- begin_iterator – This is an iterator method that points to the first element of the reverse data element range.
- end_iterator – This is an iterator method that points after the last container element of the reverse data element range.
Reverse Algorithm Effect.
This algorithm reverses the data elements in their place within the container data type element, which means it directly modifies the container data type element.
Example of the Reverse Algorithm.
#include <iostream>
#include <vector>
#include <algorithm> // Here we use the algorithm header file for the std::reverse method
using namespace std;
int main() {
vector<int> vctr = {8, 9, 2, 1, 7, 4, 6};
// Here it reverses the vector data element container in place
reverse(vctr.begin(), vctr.end());
cout << “Reversed vector data element order is – “;
for (int data : vctr) {
cout << data << ” “;
}
cout << endl;
return 0;
}
Explanation of the Reverse Algorithm.
- In this example, the reverse function reverses the vector vctr element in place, and the storage data element order of the current vector data elements is printed in reverse order.

