# Sort in C++ Standard Template Library (STL)

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Here we will see how to use the sort() function of C++ STL to sort an array So if the array is like A = [52, 14, 85, 63, 99, 54, 21], then the output will be [14 21 52 54 63 85 99]. To sort we will use the sort() function, that is present in the header file <algorithm>. The code is like below −

## Example

Live Demo

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {52, 14, 85, 63, 99, 54, 21};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Array before sorting: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
sort(arr, arr + n);
cout << "Array after sorting: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}

## Output

Array before sorting: 52 14 85 63 99 54 21
Array after sorting: 14 21 52 54 63 85 99
Updated on 18-Dec-2019 10:09:23