Updated June 20, 2023
Introduction to C++ sort()
The sort() function in C++ sorts a number of elements or a list of elements within the first to last elements in an ascending or descending order. This function executes the sorting operation within the specified list or range, starting with the first element and ending with the last element. The sort function takes in two parameters as arguments and returns the result of sorting. Comparing the elements is the main operation. There can be exceptions in case the elements that are being compared encounter any exceptions. The most common ways to sort are in either an ascending order or a descending order.
Syntax and parameters:
The standard syntax for the sort function includes of sort keyword and two parameters. The return output will be the result of the sort operation.
void sort(RIt first, RIt last);
- The parameters implemented in the above syntax are RandomIt, first and last.
- Here the first and last are the range between whose sorting is to be done; the first notifies the first element of the list, while the last denotes the last element.
How sort() Algorithm Function work in C++?
- The basic method by which the sorting algorithm works is based on comparison.
- The sorting function attempts to compare each and every element of the list.
- The comparison works in a way as comparing the first element with the second element, followed by the second and third, and so on. Here the comparison operator “<” is widely used.
- So, simply speaking, the sort function at a time picks two values or elements of a list, then compare these two values to identify smaller and bigger value and arrange them in a form, ascending or descending, whichever is required.
Examples of C++ sort()
We will have a list of values, which will be unsorted and not in any order, and we will aim to correct the list by implementing the sort function.
Example #1
Our first example takes in an array of number values, unsorted, and we will implement the sort, and the output will be a list of values in a sorted format.
Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr1[] = {99,91,94,96};
int n = sizeof(arr1)/sizeof(arr1[0]);
sort(arr1, arr1+n);
cout << "\n List of Array after sorting is: ";
for (int i = 0; i < n; ++i)
cout << arr1[i] << " ";
return 0;
}
Explanation:
- We have our system files and namespace, followed by initializing the main code. Then we have our first array variable of integer type, which holds a list of four numbers totally unsorted, following no order.
- Then we have another integer variable followed by the sort function. We then print s statement, which then comes in our for statement, which will pick and print the numbers in a sorted format.
- Upon successful execution, the output of the program will be a statement and a list of four numbers in a sorted format.
Output:
As explained and expected, the program generates an output statement followed by the corrected and sorted list of numbers. The sorting operation is performed in ascending order.
Example #2
For our next example, we have a list of unsorted number values, and we will implement a simple sort function to sort these values and print; for that, the code is as follows.
Code:
#include <iostream>
#include <algorithm>
using namespace std;
void show(int a[]) {
for(int b = 0; b < 10; ++b)
cout << a[b] << " ";
}
int main() {
int a[10]= {3,4,1,0,5,7,8,6,9};
cout << "\n This is the unsorted list: ";
show(a);
sort(a, a+10);
cout << "\n\n Here's the output of sort operation:: ";
show(a);
return 0;
}
Explanation:
- Started with required system files followed by declaring the login for the program. Then we have our main block of code, where we have an array with integer values followed by a print statement.
- Then we have our sort function and parameter values passed, followed by the print statement and the final output.
- The expected output is two statements, which include a list of the unsorted array and another list which is the output of the sorting operation.
Output:
Our code executed as expected, and the output is proper.
Example #3
For our next example, we will sort the list of values in two different methods.
Code:
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
int main() {
std::array<int, 10> list = {9, 6, 3, 5, 7, 4, 2, 8, 1, 0,};
std::cout << '\n';
std::cout << "Sorting done with simple < operator: ";
std::cout << '\n';
std::sort(list.begin(), list.end());
for (auto a : list) {
std::cout << a << " ";
}
std::cout << '\n';
std::cout << "\n This is output of sorting using custom function: ";
std::cout << '\n';
struct {
bool operator()(int a, int b) const {
return a < b;
}
} customLess;
std::sort(list.begin(), list.end(), customLess);
for (auto a : list) {
std::cout << a << " ";
}
std::cout << '\n';
}
Explanation:
- Like the earlier example, we begin with the required system files and a main block. Then we have our array of values, followed by a few lines of code for output, where we first print the sorted list as a result of our sort operation. Then within our second method, we have a custom function, which creates a struct and passes the values.
- Then we have our sort function, which starts with begin, then the end, and the custom function.
Output:
As expected, the output of the code is that the sorting has been done in two different formats using two methods.
Conclusion
In C++, we use the sort() function to sort a list of values. This function allows us to perform sorting in either ascending or descending order. We saw a few methods and ways to implement the sort function.
Recommended Articles
This is a guide to C++ sort(). Here we discuss the introduction to C++ sort(), how sort() algorithm function works, along with programming examples. You may also have a look at the following articles to learn more –