Table of contents
1.
Introduction
2.
What is the sort function in C++?
2.1.
Syntax
3.
Sorting in Ascending Order
3.1.
Code
3.2.
C++
3.3.
Input
3.4.
Output
4.
Sorting in Descending Order
4.1.
Code
4.2.
C++
4.3.
Input
4.4.
Output
5.
Sorting in Particular Order
5.1.
Code
5.2.
C++
5.3.
Input
5.4.
Output
6.
Complexity
7.
Frequently Asked Questions
7.1.
What is the sort function in C++?
7.2.
What is the use of sort () function?
7.3.
What is the function sort vector in C++?
7.4.
What is the selection sort function in C++?
7.5.
How many types of sort are there in C++?
8.
Conclusion
Last Updated: Dec 3, 2024
Easy

Sort() Function in C++

Author Mayank Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

One of the most fundamental and practical operations performed on data is Sorting. It seeks to arrange data in a specific way, which may rise or reduce depending on the needs. 

Sort Function in C++

The C++ STL includes a built-in function called "sort()" that makes it simple to conduct sorting algorithms. Without wasting time, let’s see what’s array sort function in c++.

Also see, Literals in C

What is the sort function in C++?

C++ STL provides a vector/array sort function in C++ that sorts a vector or array (items with random access). It typically requires two parameters: the length up to which we want the array or vector to be sorted and the position in the array or vector from which the sorting must start. Use the third optional parameter if you wish to sort the components lexicographically.

Syntax

sort(start address, end address)
You can also try this code with Online C++ Compiler
Run Code


Start address => The first address of the element.

Last address => The address of the next contiguous location of the last element of the array.

You practice by yourself with the help of online c++ compiler.

Sorting in Ascending Order

By default, the array sort function in c++ sorts the elements in ascending order. Below is a simple code to show the working of sort(). 

Code

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
   cin>>n;
   int arr[n];
   for(int i=0;i<n;i++)
   {
     cin>>arr[i];
   }

   sort(arr, arr + n);

   cout << "\nArray after sorting using "
           "default sort is : \n";
   for (int i = 0; i < n; ++i)
       cout << arr[i] << " ";

   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

n=6
7 5 3 1 2 4

Output

output

Sorting in Descending Order

The third parameter of the array sort function in c++ is used to determine the order in which the components should be sorted. The "greater()" function can sort the results in descending order. This method compares items in a way that prioritizes more important elements first.

Code

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
   int arr[n];
   for(int i=0;i<n;i++)
   {
     cin>>arr[i];
   }

  sort(arr, arr + n, greater<int>());


cout << "Array after sorting : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

n=6
7 5 3 1 2 4

Output

output

Read More - Time Complexity of Sorting Algorithms

Sorting in Particular Order

We can also write our comparator array sort function in c++and pass it as a third parameter. The “comparator” function returns a value of type bool, which tells us whether the first argument passed should be placed before the second argument or not.

Code

  • C++

C++

#include <bits/stdc++.h>
using namespace std;


template <class T> bool func(T x1, T x2)
{
return x1 <= x2;
}


int main()
{
   int n;
   cin>>n;
   int arr[n];
   for(int i=0;i<n;i++)
   {
     cin>>arr[i];
   }
   sort(arr, arr+n, func<int>);
cout<< "The array after sorting is:"<<endl;


for (int i = 0;i<n; i++) {
cout << arr[i] << " ";
}


return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

n=6
7 5 3 1 2 4

Output

output

Complexity

 

 

 

 

The array sort function in c++ takes O(log N) time and O(log N) additional space.

Also check out this article - Pair in C++

Frequently Asked Questions

What is the sort function in C++?

The sort function in C++ is a standard library function used to sort elements in a container, such as arrays or vectors, in ascending order based on their values.

What is the use of sort () function?

The sort() function is used to arrange elements in ascending order within a container, such as arrays or vectors, allowing for efficient data organization and retrieval.

What is the function sort vector in C++?

The sort function in C++ can be applied to vectors to rearrange their elements in ascending order, facilitating efficient data manipulation and retrieval operations.

What is the selection sort function in C++?

The selection sort function in C++ is a sorting algorithm that iteratively selects the minimum element from an unsorted portion of the array and swaps it with the element at the beginning of the unsorted portion until the entire array is sorted.

How many types of sort are there in C++?

In C++, there are various types of sorting algorithms, including bubble sort, insertion sort, selection sort, merge sort, and quick sort, each with unique approaches to arranging elements in ascending or descending order.

Conclusion

Let's sum up the article. This article examined the various circumstances and use cases of the C++ array sort function. All of that is covered in the article. I hope that everyone like it.

If you want to learn more, check out our articles on Construct the Full K-ary Tree from its Preorder TraversalRegular and Bipartite graphsWhat Is Web2Py?Why To Use Web2py?Postbacks and Internationalization in web2pyThird Party Modules In Web2pyTasks In Web2py, and  XML in Web2py.

Recommended Problems - 

Happy Coding!

Live masterclass