Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• # Sort function in C++

• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 421
Comment on it

Sort function is used to sort a given range in ascending order. This function is defined in the header algorithm. There are two version are defined of it.

```template< class RandomIt >
void sort( RandomIt first, RandomIt last );

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
```

The first version sorts the range in ascending order. Second version uses comparison function comp in which sort function works according to comp function.

first
It is the initial position of the range which is included in the range.

last
It is the position of the range which is excluded in the range.

comp
It is the comparison function, it returns a value convertible to bool.

Sample program using the first version

```#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
vector <int> v;
vector<int> :: iterator it;

for (int i = 10; i >= 0; i--) {
v.push_back(i);
}

//Before sort printing the elements of vector
for (it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;

sort(v.begin(),v.end()); //applying first version of sort (the default sort)

//After sort printing the elements of vector
for (it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;

return 0;
}
```

Output of the above program

```Before applying Sort Function
Output : 10 9 8 7 6 5 4 3 2 1 0
After applying Sort Function
Output : 0 1 2 3 4 5 6 7 8 9 10
```

Sample program using the second version

```#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
vector <int> v;
vector<int> :: iterator it;

for (int i = 0; i <= 10; i++) {
v.push_back(i);
}

cout << "Before applying Sort Function\n";
cout << "Output : ";
for (it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;

struct {
bool operator()(int a, int b)
{
return a > b;
}
} comp;

// sort function using the second version
sort(v.begin(),v.end(),comp);

cout << "After applying Sort Function\n";
cout << "Output : ";
for (it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;

return 0;
}
```

Output of the above program

```Before applying Sort Function
Output : 0 1 2 3 4 5 6 7 8 9 10
After applying Sort Function
Output : 10 9 8 7 6 5 4 3 2 1 0
```

Time Complexity of sort function is

```O(N log N)
```

## 0 Comment(s)

OR
OR
Register

OR
Fill out the form below and instructions to reset your password will be emailed to you:

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General