How to Sort a Deque in C++? Last Updated : 26 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the STL provides a container called a double-ended queue which is popularly known as deque. This container allows fast insertions and deletions at both ends of the container. In this article, we will learn how to sort a deque in C++. Example: Input: myDeque = {30, 10, 20,50,40} Output: 10 20 30 40 50Sort Elements in a Deque in C++We can use the std::sort method provided by the STL library to sort a std::deque in C++. This function sorts the elements in a given range into ascending order by default. Syntax of std::sortsort(deque.begin(), deque.end()) where begin and end are iterators denoting the beginning and the end of the deque. If we want to change the order, we can provide the custom comparator as third parameter. C++ Program to Sort a Deque The following code demonstrates how we can sort a deque in C++: C++ // C++ program to illustrate how to sort a deque #include <algorithm> #include <deque> #include <iostream> using namespace std; int main() { // Initialize a deque deque<int> dq = { 30, 10, 20, 50, 40 }; // Print the original deque cout << "Original deque: "; for (int x : dq) { cout << x << " "; } cout << endl; // Sort the dequue sort(dq.begin(), dq.end()); // Print the sorted deque cout << "Sorted deque: "; for (int x : dq) { cout << x << " "; } cout << endl; return 0; } OutputOriginal deque: 30 10 20 50 40 Sorted deque: 10 20 30 40 50 Time Complexity: O(N logN) where N is the number of elements in the deque.Auxiliary Space: O(logN) Comment More infoAdvertise with us Next Article How to Sort a Deque in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ STL cpp-deque CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Reverse a Deque in C++? In C++ STL, we have a container called deque(short for double-ended queue) that allows fast insertion and deletion operations at both the beginning and end. In this article, we will learn how to reverse a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5}; Output: Reversed Deque: 5 4 3 2 1Rever 2 min read How to Create a Deque of Sets in C++? In C++, a container called deque is a queue like container but allows for fast insertions and deletions at both ends. In this article, we will learn how to create a deque of sets in C++. Example: Input: mySet1 = {1, 4, 8, 9, 11} mySet2 = {1, 2, 3, 5, 7} Output: myDeque: [ {1, 4, 8, 9, 11}, {1, 2, 3, 2 min read How to Declare a Deque in C++? In C++, deques are sequence containers in which a user can insert data at both the front and the end of the container. In this article, we will learn how to declare a deque in C++. Declaring a Deque in C++Deque is defined as the class template std::deque in the <deque> header file. So to creat 2 min read How to Create a Stack of Deque in C++? In C++, the stack is a container in which new elements are added from one end (top) and removed from that end only whereas a deque (double-ended queue) are sequence container with the feature of expansion and contraction on both ends. In this article, we will learn how to create a stack of deque in 2 min read How to Compare Two Deques in C++? In C++ the Standard Template Library (STL) provides a container called deque (short for double-ended queue) that allows fast insertions and deletions at both ends of the deque. In this article, we will learn how to compare two deques in C++. Example: Input: deque1 = {10,20,30}; deque2 = {10,20,30}; 2 min read Like