About 36,500 results
Open links in new tab
  1. 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 order

    for (int i : arr)
    cout << i << " ";
    return 0;
    }
    Copied!

    Output:

    1 2 3 4 5
    Copied!

    Time 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!

    Manual 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!

    Time Complexity: O(n²) – suitable for small datasets.

    Other Common Methods

    Feedback
  2. 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 values
    std::sort(v.begin(),v.end());
  3. People also ask
  4. 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...
    See more on en.cppreference.com
  5. 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 …

  6. 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 …

  7. 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 …

  8. 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 …

  9. 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 …

  10. 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 …

  11. 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, …