C++ sort uses two arguments to sort

Sorting is often used in programming. Bubble sorting has high time complexity. The C++ library function sort can be used to sort quickly.

1. The required header files #include <algorithm> and using namespace std; 
2. The time complexity is n*log2(n)

3. The Sort function has three parameters:

(1) The first is the starting address of the array to be sorted .

(2) The second is the ending address (the last address to be sorted)

(3) The third parameter is the sorting method, which can be from large to small or from small to large, and you can also not write the third parameter. At this time, the default sorting method is from small to large.

#include <iostream>
#include <algorithm>
intmain()
{
 int a[20]={2,4,1,23,5,76,0,43,24,65},i;
 for(i=0;i<20;i++)
  cout<<a[i]<<endl;
 sort(a,a+20);
 for(i=0;i<20;i++)
 cout<<a[i]<<endl;
 return 0;
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324702248&siteId=291194637