Site icon WGLabz

Sort using algorithm header

The below program takes an array as input and prints the sorted array. The header <algorithm> defines a collection of functions especially designed to be used on ranges of elements.sort function takes two arguments. The arguments are the start index and end index of array. This header file does not work in c. It can be only used in C++.

Output : 

Code :

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
int n;
cout<<"Enter number of elements you want to sort : ";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;

}

Exit mobile version