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

C++Server Side ProgrammingProgramming

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
raja
Published on 01-Apr-2020 06:31:06
Advertisements