Internal details of std::sort() in C++


In this tutorial, we will be discussing a program to understand the internal details of std::sort() in C++.

std::sort() function is used to sort down an array using a comparison of the elements. If we look at the in-depth functionality of std::sort() it uses IntroSort algorithm to sort the elements of a container object.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
   int n = sizeof(arr)/sizeof(arr[0]);
   sort(arr, arr+n);
   cout << "\nArray after sorting using " "default sort is : \n";
   for (int i = 0; i < n; ++i)
      cout << arr[i] << " ";
   return 0;
}

Output

Array after sorting using default sort is :
0 1 2 3 4 5 6 7 8 9

Updated on: 01-Apr-2020

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements