- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
Sorting in C++ can be done efficiently using the Standard Template Library (STL) std::sort() function, which by default arranges elements in ascending order using a hybrid of quicksort, heapsort, and insertion sort for optimal performance.
Example – Using std::sort()
#include <bits/stdc++.h>using namespace std;int main() {int arr[] = {5, 4, 1, 2, 3};int n = sizeof(arr) / sizeof(arr[0]);sort(arr, arr + n); // Ascending orderfor (int i : arr)cout << i << " ";return 0;}Copied!✕CopyOutput:
1 2 3 4 5Copied!✕CopyTime Complexity: O(n log n) Space Complexity: O(log n)
Sorting in Descending Order You can pass a comparator like greater<int>() to sort in reverse.
sort(arr, arr + n, greater<int>());Copied!✕CopyManual Sorting – Bubble Sort If STL is not allowed, you can implement sorting manually:
void bubbleSort(int arr[], int n) {for (int i = 0; i < n - 1; i++)for (int j = 0; j < n - i - 1; j++)if (arr[j] > arr[j + 1])swap(arr[j], arr[j + 1]);}Copied!✕CopyTime Complexity: O(n²) – suitable for small datasets.
Other Common Methods
How to Sort an Array in C++? - GeeksforGeeks
Jul 23, 2025 · Sorting an array involves rearranging its elements in a specific order such as from smallest to largest element or from largest to smallest element, etc. In this article, we will learn how …
See results only from geeksforgeeks.orgsort () in C++ STL
The sort () function is used to sort elements in a container or array. It provides a simple and efficient way to sort data in C++. Works on random-access iterators …
How to use std::sort to sort an array in C++ - Stack …
Mar 12, 2017 · C++ is providing you a function in STL (Standard Template Library) called sort which runs 20% to 50% faster than the hand-coded quick-sort. Here is …
Code sample
#include <algorithm>#include <array>std::array<int, 2000> v;// Fill the array by valuesstd::sort(v.begin(),v.end());- People also ask
std::sort - cppreference.com
- Before LWG713, the complexity requirement allowed sort() to be implemented using only Quicksort, which may need O(N2 )comparisons in the worst case. Introsort can handle all cases with O(N·log(N)) comparisons (without incurring additional overhead in the average case), and thus is usually used for implementing sort(). libc++ has not implemented the...
How to Sort arrays, vectors, and strings in C++ with Code Examples
The C++ Standard Library provides the std::sort function from the <algorithm> header to sort arrays, vectors, strings and more. sort takes three parameters: the start of a sequence to sort, the end of the …
C++ Algorithm sort () Function - W3Schools
The sort() function sorts the elements of a data range in ascending order. The range of data is specified by iterators. Required. An iterator pointing to the start of the data range to be sorted. Required. An …
How to use std::sort to sort an array in C++ - Online …
Sorting of an array refer to the process of arranging the elements of the array in ascending or descending order. In this article, we will learn how to use the …
std::sort - cppreference.net
Feb 10, 2025 · Sorts the elements in the range [ first , last ) in non-descending order. The order of equal elements is not guaranteed to be preserved. 1) Elements are sorted with respect to operator < (until …
sort - C++ Users
Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go …
sort () in C++ STL - GeeksforGeeks
Oct 10, 2025 · The sort () function is used to sort elements in a container or array. It provides a simple and efficient way to sort data in C++. Works on random-access …
9 Ways To Sort Array In C++ (Explained With Code …
Sorting an array in C++ entails arranging all its elements in a particular sequence. Depending on the specific requirements, you can use multiple sorting algorithms, …