Difference between std::sort and std::stable_sort() function in C++20 May 2025 | 4 min read In this article, we will discuss the difference between std::sort() and std::stable_sort() in C++. Before discussing their differences, we must know about the std::sort() and std::stable_sort() with their syntax, parameters, and examples. What is the std::sort() function in C++?In C++ programming, the std::sort() function is the built-in function in the Standard Template Library (STL). It is mainly utilized to sort the elements in the range [first, last] in a desired order. It gives an easy and effective way to sort the data in C++. It may also be utilized for custom sorting by specifying in a comparator function that returns a Boolean value. This std::sort() function is defined in the C++’s <algorithm> header file. It only works with data structures that allow random access to their components, like vectors and arrays. Syntax:It has the following syntax: Parameters:
Example:Let us take an example to illustrate the std::sort() function in C++. Output: 19 22 37 44 45 55 68 What is the std::stable_sort() function in C++?The std::stable_sort() function is also utilized to sort the elements in the range [first, last] in the ascending order. It is similar to the std::sort() function, but it preserves the desired order of equivalent elements. This std::stable_sort() function is defined in the C++’s <algorithm> header file. Syntax:It has the following syntax: and the other syntax is: Parameters:
Return Value:It does not return any value. Example:Let us take an example to illustrate the std::stable_sort() function in C++. Output: Sorted Employees: Bob (Dept 1) David (Dept 1) Alice (Dept 2) Charlie (Dept 2) Grace (Dept 2) Eve (Dept 3) Frank (Dept 3) Key differences between std::sort and std::stable_sort() function in C++:![]() There are several key differences between std::sort() and std::stable_sort() in C++. Some main differences are as follows:
Conclusion:In conclusion, the std::sort() and std::stable_sort() functions are important functions in C++. Both functions are used to sort the elements in a desired order. Understanding the differences between these algorithms assists in picking the best one for our requirements. It ensures the efficiency and correctness of our program. We may get the best algorithm according to our requirements. |
The Juggling Algorithm is a useful C++ technique that does rotations by shifting the elements around. It divides the array into sets using the Greatest Common Divisor (GCD) of the array size n and number of spots d to be rotated. After that, the elements are...
5 min read
Introduction: The Flyweight pattern is one of the structural design patterns described by the Gang of Four. It's used when you need to support many fine-grained objects efficiently. This pattern aims to minimize memory usage or computational expenses by sharing as much as possible with similar objects....
14 min read
Introduction: In traversing a binary tree it involves the visiting of the all given nodes in a systematic order. Anti-clockwise spiral traversal is the only way to traverse a binary tree. This traversal begins at the root and goes to the leftmost leaf, then to the...
11 min read
Introduction: "Rearrange Distant Barcodes" is a computational problem often encountered in the field of computer science, particularly in algorithm design and optimization. The challenge involves reorganizing a sequence of barcodes, represented by integers, such that no two adjacent barcodes are identical. This problem is akin to finding...
15 min read
Certain mathematical concepts are great examples in programming, and "nude numbers" form one such series. Even if the term is interesting, it goes very deep and has the very essence of mathematical elegance written in the language of simplicity. This article explores an idea as...
4 min read
In this article, we will discuss the differences between Delete and Delete[] in C++. Before discussing their differences, we must know about Delete and Delete[] in C++ with their examples. Overview of Delete: Delete can be used to deallocate memory that was dynamically created for a single object...
4 min read
Differences between C++ and Lua In this article, we will discuss the differences between C++ and Lua. Before discussing their differences, we must know about C++ and Lua with their features. What is the C++? C++ is a strongly typed, compiled language that supports procedure, object-oriented, and Generic...
4 min read
Mathеmatics is еssеntial to programming because it allows for thе еxеcution of numerous calculations and operations. The Sqrtf() function is one such crucial function in the C++ programming language. This function is crucial when calculating thе squarе root of a givеn valuе, еspеcially for floating-point...
4 min read
In this article, we will discuss how to encode a sentence into Pig Latin in C++ with several example. Pig Latin encryption is a technique to encode normal sentences into abnormal sentences. The rules to convert a particular sentence to Pig Latin are: First, divide the sentence into...
4 min read
Introduction Recreating simulations of an asteroid collision is a highly intriguing area of practice where theory meets application. Asteroids, the remains of cosmic events, frequently collide with each other. Syntax: Classes: The class will be used to assign properties like the non-volatile data of position, velocity, mass, and radius...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India