Sort in C++ Standard Template Library (STL)

Sorting is one of the most basic functions applied to data. It means arranging the data in a particular fashion, which can be increasing or decreasing. There is a builtin function in C++ STL by the name of sort().
Internally this function is implemented as Quick-sort. The complexity of it is O(N*log(N)).
The prototype for sort is :

sort(startaddress, endaddress)

startaddress: the address of the first element of the array
endaddress: the address of the last element of the array
#include <iostream>
#include <algorithm>

using namespace std;

void show(int a[])
{
    for(int i = 0; i < 10; ++i)
        cout << a[i] << " ";
}

int main()
{
    int a[10]= {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
    cout << "\n The array before sorting is : ";
    show(a);

    sort(a, a+10);

    cout << "\n\n The array after sorting is : ";
    show(a);

    return 0;

}

 
The outut of the above program is :

The array before sorting is : 1 5 8 9 6 7 3 4 2 0
The array after sorting is : 0 1 2 3 4 5 6 7 8 9

 

Refer std::sort() for more details.



My Personal Notes arrow_drop_up



Practice Tags :
Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.2 Average Difficulty : 1.2/5.0
Based on 61 vote(s)






User Actions