

- 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 use std::sort to sort an array in C++
In programming language, sorting is a basic function which is applied to data to arrange these data is ascending or descending data. In C++ program, there is a function std::sort() for sorting the array.
sort(start address, end address)
Here,
Start address => The first address of the element. Last address => The address of the next contiguous location of the last element of the array.
Example Code
#include <iostream> #include <algorithm> using namespace std; void display(int a[]) { for(int i = 0; i < 5; ++i) cout << a[i] << " "; } int main() { int a[5]= {4, 2, 7, 9, 6}; cout << "\n The array before sorting is : "; display(a); sort(a, a+5); cout << "\n\n The array after sorting is : "; display(a); return 0; }
Output
The array before sorting is : 4 2 7 9 6 The array after sorting is : 2 4 6 7 9
- Related Questions & Answers
- How to sort an array in C#?
- Using merge sort to recursive sort an array JavaScript
- C program to sort an array by using merge sort
- How to sort an array elements in android?
- Internal details of std::sort() in C++
- Write a Golang program to sort an array using Bubble Sort
- Sort an Array in C++
- How to sort an Array using STL in C++?
- How to use MongoDB Aggregate to sort?
- Sort an array according to another array in JavaScript
- Sort an Array of string using Selection sort in C++
- Easiest way to sort an array in MongoDB
- How to sort an array of integers correctly JavaScript?
- How to use the Sort() method of array class in C#?
- Use array as sort order in JavaScript
Advertisements