![]() |
In C++, a list is a sequence container provided by the STL library of C++ that provides the features of a doubly linked list and stores the data in non-contiguous memory locations efficiently. In this article, we will learn how to sort a list in C++. Example: Input: Sort a List in C++To sort a std::list in C++, we can use the std::list::sort function. This function sorts the elements stored in a list by changing their position within the list only as a result the original list is modified but the order of equal elements remains preserved. Syntax to Sort a List in C++list_Name.sort(); Here, list_Name is the name of the list container and no parameters are passed. C++ Program to Sort a ListThe below program demonstrates how we can use the list::sort() to sort a list in C++.
Output List before sorting : 30 10 20 40 50 List after sorting : 10 20 30 40 50 Time Complexity: O(N logN), here N is the size of the list. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |