How to use Sort function in c++ language and sort the array element
Use Sort( ) Function in c++ Language
As we know, when we sort the array element, we use the 5 to 6 line write and we increase the time complexity in our code so the solution is that we use the sort( ) which is one of the functions of Algorithm header file
Syntax of Sort( ) function -
Sort( first element address, last element or contiguous location element address)
Code -
#include<algorithm>
using namespace std;
int main()
{
int n;
cout<<"Enter a number: ";
cin>>n;
int a[n];
cout<<"Enter a "<<n<<" number: ";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Your sort element is : ";
sort(a, a+n);
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
Sample Output -
Enter a number: 5
Enter a 5 number:
2
5
3
1
4
Your sort element is: 1 2 3 4 5
--------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment