
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- How to sort an array in C#?
- Using merge sort to recursive sort an array JavaScript
- How to sort an array elements in android?
- C program to sort an array by using merge sort
- How to sort an Array using STL in C++?
- Golang Program To Sort An Array
- Golang Program To Sort An Array In Ascending Order Using Insertion Sort
- Golang Program to sort an array in descending order using insertion sort
- Internal details of std::sort() in C++
- Write a Golang program to sort an array using Bubble Sort
- How to use the Sort() method of array class in C#?
- Sort an array according to another array in JavaScript
- How to sort an array with customized Comparator in Java?
- How to sort an array of integers correctly in JavaScript?
- How to sort an array in JavaScript?Explain with example?

Advertisements