Introduction to C++ sort()
The sort() function in C++ is used to sort a number of elements or a list of elements within first to last elements, in an ascending or a descending order. Here we have a range for a list, which starts with first element and ends with last element and the sorting operation is executed within this list. The sort function takes in two parameters as argument and returns the result of sorting. Comparing the elements is the main operation. There can be exceptions in case if the elements that are being compared encounter any exception. The most common ways to sort are in either an ascending order or a descending order.
Syntax and parameters:
The standard syntax for 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 the sorting is to be done, the first notifies the first element of the list while 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 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 a bigger value and arrange them in a form, ascending or descending, whichever is required.
Examples of C++ sort()
Given below are the examples mentioned :
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 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, 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 output is a statement followed by the corrected, sorted list of the numbers that we passed. Here our sorting was executed in an ascending form.
Example #2
For our next example, we have a list of number values, that are unsorted 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 a 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 statement, which includes a list of unsorted array and another list which is output of 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:
- Similar to earlier example, we begin with required system files and a main block. Then we have our array of values, followed by few lines of code for output, where we first print the sorted list as a result of our sort operation.
- Here, the sorting is done as begin to end. 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. Finally, all our results are printed and can be seen as they are in the below attached screenshot.
Output:
As expected, the output of the code is that the sorting has been done in two different formats using two methods.
Conclusion
The sort function of C++ is used to sort a list of values. The sorting can be done in ascending or descending way. The sorting is basically done by comparing two values. We saw 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 work in along with programming examples. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses