# sort() Function in C++

## Overview

Sorting is a technique used to arrange the numbers or elements inside the array or vector in ascending or descending order. To perform the sorting operation we can use the sort function in C++.

The sort function in C++ is a part of STL library of C++. The sort() function is unstable while the function stable_sort() is stable but it is faster.

## Library of sort() Function in C++

The sort function in C++ is a part of STL library of C++. To use the sort function in our code we need to include the algorithm library in the code as #include <algorithm>.

## Syntax of sort() Function in C++

**The syntax of the sort function in C++ is as follows -**

## Parameters of sort() Function in C++

### first

It is a random access iterator that is pointing to the first element of the given range of array or vector. It is the address of the first element in given range and it is inclusive in the range.

### last

It is a random access iterator that is pointing to the element which is next to the last element of given range of array or vector. It is the address of the element next to the last element. It is exclusive in the range that's why an element at this position not taken into the consideration for sorting operations.

### comp

It is user defined binary predicate function which takes two arguments of same datatype as elements of the array/vector and returns the true or false after comparison.

### greater<data_type>

It is used to sort the array/vector in decreasing order. It is a pre-defined function with data_type same as the elements of the given container.

## Return Value of C++ Algorithm sort()

It will not return any value as it has void as a return type shown in the above syntax.

## How Does sort() Algorithm Function Work in C++?

The sort function in C++ uses the IntroSort internally which is a kind of hybrid sort. IntroSort is a combination of Quick Sort, Heap Sort, and Insertion Sort. By default, it chooses to perform sorting with QuickSort but in unfair conditions, like taking more time than N * log(N) it switches to the Heap Sort or when the array size becomes very small, it switches to the Insertion Sort.

## Complexity of C++ Algorithm sort()

The average complexity is N * log(N), where N is the difference between last and first(i.e. last - first) or the number of elements of an array or vector to sort.

## Data Races of C++ Algorithm sort()

The object in the range [first, last) gets modified where the first is inclusive and the last is exclusive in this range.

## Exceptions of C++ Algorithm sort()

An exception is thrown by the sort function in C++ if any of the element comparisons, the element swap, or an operation on the iterator throws an exception.

## Examples

### 1. Use of Sort Function in C++

By default sort function will sort the data in ascending order if we just passed the first and last as shown in the below code example.

**Code Example:**

**Output:**

### 2. To Sort Data in Descending Order

To sort the data in descending order we need to make use of the binary predicate function as a third parameter of the sort function in C++.

In the following code example, we used the **greater** function which is used to perform the comparison, it helps to sort vector in the descending order.

**Code Example:**

**Output:**

### 3. To Sort Data with Custom Function

We can declare our own custom functions as well to perform the sorting of given array or vector. In the following code, we created the vector and defined our own custom function to perform the sorting with the help of absolute values in descending order.

**Code Example:**

**Output:**

## Partial Sort

The C++ STL library provides the partial_sort() function which is also used to perform the sorting operation. Unlike sort function in C++ which is used to sort the entire array we use the partial sort function to sort partial array.

In the partial sort all the elements in the range of [first, last) are considered and first (middle - first) elements are arrangnd in the ascending order, all the elements after that are arranged as they were before in the array.

**Syntax:**

**Consider the following coding example where partial sort is implemented.**

**Code Example:**

**Output:**

Consider the above code, where the first elements i.e. middle - first (elements before 3rd index) are arranged in a sorted manner. All the remaining elements after (from 3rd index) that are arranged as they are present initially in the array. Use this Online Compiler to compile your C++ code.

**Want to stand out in your coding projects? Our Free C++ course is tailor-made for college students who aspire to create remarkable projects.**

## Conclusion

- To perform the sorting operation we can use the sort function in C++.
- The sort function in C++ is the part of STL library and we can use it by including the algorithm library.
- The sort function performs sorting in the range\[first, last) where the first is included the and last is excluded.
- The sort function in C++ uses IntroSort internally with the complexity of N*log(N), where N is last - first or the total number of elements in the range.
- We can use the greater functions in C++ to perform sorting in descending order and custom functions for other purposes.
- To perform the partial sorting the partial_sort() function in C++ is used, which takes one extra argument as the address of the middle element.