

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sort an Array using STL in C++?
Here we will see how to sort an array using STL functions in C++. 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 have one function called sort() present in the header file <algorithm>. The code is like below −
Example
#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 << "\nArray 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
- Related Questions & Answers
- How to reverse an Array using STL in C++?
- Using merge sort to recursive sort an array JavaScript
- Shuffle an Array using STL in C++
- Insertion sort using C++ STL
- C program to sort an array by using merge sort
- How to sort an array in C#?
- Sort an Array of string using Selection sort in C++
- Write a Golang program to sort an array using Bubble Sort
- How to use std::sort to sort an array in C++
- Sorting an array according to another array using pair in STL in C++
- Sort an array in descending order using C#
- How to find the maximum element of an Array using STL in C++?
- How to sort a Vector in descending order using STL in C++?
- How to sort an array elements in android?
- How to sort 0,1 in an Array without using any extra space using C#?
Advertisements