
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort an Array Using STL in C++
The problem is to sort an array using C++'s Standard Template Library (STL). Sorting an array means rearranging its elements in a specific order. In this case, we aim to sort the elements in both ascending and descending order, using the built-in functionalities of the STL.
Let's say we have the following array of integers:
int arr[] = {4, 2, 9, 1, 7};
The goal is to sort this array so that the elements are ordered from smallest to largest (ascending) and largest to smallest (descending):
Ascending: arr = {1, 2, 4, 7, 9} Descending: arr = {9, 7, 4, 2, 1}
In this article, we will show how to use C++'s Standard Template Library(STL) to sort an array in both ascending and descending order.
Approaches for Sorting an Array Using C++ STL
In C++, we can sort an array in different ways using the Standard Template Library (STL). Here are four approaches to sorting an array:- Using std::sort()
- Using std::sort() with a Custom Comparator
- Using std::stable_sort()
- Using std::partial_sort()
Using std::sort()
The easiest and most efficient way to sort an array is by using the built-in std::sort() function from the C++ Standard Library.
Example
In the code below, we use std::sort() to sort the array in ascending order, placing the smallest elements first.
#include <iostream> #include <algorithm> // For sort() using namespace std; int main() { int arr[] = {5, 2, 8, 1, 9, 3}; int n = sizeof(arr) / sizeof(arr[0]); // Display the original array cout << "Original array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; // Sort the array in ascending order using std::sort sort(arr, arr + n); // Display the sorted array cout << "Sorted array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; }
Output
The output of the code displays the sorted array in ascending order as expected.
Original array: 5 2 8 1 9 3 Sorted array: 1 2 3 5 8 9
Time Complexity: O(n log n), as std::sort() is used to sort the array.
Space Complexity: O(1), since no extra space is used beyond the input array.
Using std::sort() with a Custom Comparator
If you need to sort the array in descending order, you can use std::sort() with a custom comparator. A comparator is a function that defines the sorting condition. In this case, we'll write a comparator that sorts elements in descending order.
Example
In this example, we use std::sort() with a custom comparison function compare() to sort the elements of the array in descending order. The compare() function returns true if a is greater than b, making sure the array is sorted from largest to smallest.
#include <iostream> #include <algorithm> // For sort() using namespace std; bool compare(int a, int b) { return a > b; // This will sort in descending order } int main() { int arr[] = {5, 2, 8, 1, 9, 3}; int n = sizeof(arr) / sizeof(arr[0]); // Display the original array cout << "Original array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; // Sort the array in descending order using a custom comparator sort(arr, arr + n, compare); // Display the sorted array cout << "Sorted array in descending order: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; }
Output
The output of the code displays the sorted array in descending order as expected.
Original array: 5 2 8 1 9 3 Sorted array in descending order: 9 8 5 3 2 1
Time Complexity: O(n log n), as std::sort() is used with a custom comparator for sorting.
Space Complexity: O(1), because no extra memory is used beyond the input array.
Using std::stable_sort()
std::stable_sort() works similarly to std::sort() but the difference is that it maintains the relative order of equal elements. If two elements are equal, they will maintain their original order in the sorted array. This can be important when sorting arrays with multiple properties.
Example
In the example below, we use std::stable_sort() to sort the array. The elements with equal values will stay in the same order relative to each other.
#include <iostream> #include <algorithm> // For stable_sort() using namespace std; int main() { int arr[] = {5, 2, 8, 1, 9, 3}; int n = sizeof(arr) / sizeof(arr[0]); // Display the original array cout << "Original array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; // Sort the array in ascending order using std::stable_sort stable_sort(arr, arr + n); // Display the sorted array cout << "Sorted array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; }
Output
The output of the code displays the sorted array in ascending order.
Original array: 5 2 8 1 9 3 Sorted array: 1 2 3 5 8 9
Time Complexity: O(n log n), because std::stable_sort() uses a sorting algorithm that takes this time.
Space Complexity: O(n), as it requires extra memory to sort the array.
Using std::partial_sort()
std::partial_sort() is used when you only need to sort part of the array, for example, the smallest or largest elements, without sorting the entire array. It sorts only the first k elements, leaving the rest of the array unsorted.
Example
In the following example, we use std::partial_sort() to sort only the first three smallest elements. The rest of the array is left unsorted.
#include <iostream> #include <algorithm> // For partial_sort() using namespace std; int main() { int arr[] = {5, 2, 8, 1, 9, 3}; int n = sizeof(arr) / sizeof(arr[0]); // Display the original array cout << "Original array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; // Sort the first 3 elements in ascending order using std::partial_sort partial_sort(arr, arr + 3, arr + n); // Display the partially sorted array cout << "Partially sorted array (first 3 elements): "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; }
Output
The output shows the first three smallest elements sorted in ascending order, while the rest of the array remains unchanged.
Original array: 5 2 8 1 9 3 Partially sorted array: 1 2 3 8 9 5
Time Complexity: O(n log k), where n is the total number of elements and k is the number of elements to sort.
Space Complexity: O(1), as no extra space is used.
Conclusion
In this article, we've seen that C++ STL offers several ways to sort arrays. We can use std::sort() for most cases, std::stable_sort() for when we need stable sorting, and std::partial_sort() if we only want to sort part of the array. These methods give us the flexibility and speed needed for different situations.