 # Sort Vector in C++: Ascending and Descending Order

hiDo you know what is Sort Vector in C++ programming language? Have you ever thought about how to sort a vector in CPP? Let us try to understand the sort vector in CPP & how to sort a vector in CPP.

But before we start writing about the sort vector in CPP, we need to first understand its background of it using one daily life example. This example will help to understand the necessity of using a sort vector in C++ programming language.

Let us assume one scenario.

Suppose, as a student of computer science, your teacher provided you with one assignment. You need to complete the assignment using the C++ programming language. In that assignment, you have to sort some numbers. Your teacher can provide thousands of the numbers based on that you need to sort it. As per your choice, you can sort it in any format.

So, what will you do in such a case? Will you implement a loop to get the answer to it?

If you use a for loop to get the answer to it, you will commit one blunder. Suppose, your teacher provided a hundred elements to sort. Now, that for loop will take unlimited time to sort that size of elements. In those cases, you need to use a sort vector in CPP. Sort vector in C++ programming language is used in such cases, where there is a large number of data present.

But before we know how to sort a vector in CPP, let us try to know what is sort vector briefly. ## What Is Sort Vector In C++ Programming Language? Read Below

Before we know the sorting process, we need to know the vector process in C++ programming language. Vector is a special type of data structure present in C++ programming language. It is similar to the array concept. But there are some more features present in the vector concept. In the array, there is no provision to increase the size of the array. But in the case of vectors, there is a concept to increase the size of the vector. In the runtime of the program execution, the memory can be increased.

The memory of the vector can be dynamically increased. The elements of the vector will be placed continuously in the memory. Sort is a function that can act on the vector. Not on the vector, but the sort function can be used for any data structure. The sort function is not used to return anything. From any given index number, it can sort the elements within the seconds. That is the reason; the sort function is used to format the data in the given order.

There is a total of three arguments that can be passed to the sort function. Among them, the first two arguments are related to the starting & ending point of the vector. The first argument will be the starting number of the vector. And the second argument will be the ending index of the vector. There is also another argument present, that determines the format of the sorting. This means, there are two ways a vector can be sorted using that third argument.

## What Are The Types Of Sorting Using Sort Vector In C++ Programming Language? Get To Know

So, after getting the basic knowledge of the sort vector in C++ programming language, it is time to know the types of sorting. Mainly, there are two types of sorting are present. Depending upon the use of the third argument in the soft function, there are two possible ways of sorting.

Let us make a list of the types of sorting using sort vector in C++ programming language:

1. Ascending Order
2. Descending Order

Let us know the implementation process of each & every type one by one briefly.

### Implementation Of Sort Vector In C++ Programming Language (Ascending Order):

In the ascending order format, there is no need to mention the third argument in the sort vector function. Rather, we need to keep it blank. By default, it will be taken as the ascending order declaration. Let us try to know the implementation process.

Here, first, we need to declare one integer vector. As well as we need to provide value to it. After that, we need to use the sort vector function. There we need to provide two arguments. The first one will be the beginning index of the vector. And the second one will be the ending index of the vector. Then we can proceed further.

After that, we need to use the for each loop to print all the data. As of now, all the data is in ascending order. If the vector is now printed, then we will find the ascending order.

General Syntax: sort(vector-name.begin(),vector-name.end());

Example:

```#include <bits/stdc++.h> using namespace std;

int main(){

vector<int> zap{ 10, 80, 90, 46, 57, 30, 14, 222, 100 }; //Declaration Of Vector

sort(zap.begin(), zap.end()); // Using Sort Function For Ascending Order

cout << "Ascending Order: \n";

for (auto coding : zap) // Printing Vector

cout << coding << "-";

return 0;}```

Let us try to find out the output of the above code. It will help to find out the sort vector in C++ programming language.

Output: ### Implementation Of Sort Vector In C++ Programming Language (Descending Order):

Now, for the descending order, there is a need to declare the third argument in the sort function. This argument will help to declare that, this sort of function will not work as the default one. In this case, we need to write the greater<int>(), as the third argument. This argument is used to declare that the greater one will be the first one.

Again, we need to declare the vector for this purpose. We need to also provide value to it & we need to declare it. Then we need to use the sort function. There we need to provide the same two arguments. Along with that, there should be the third argument need to be present.

After that, we need to use the for each loop to print all the data. Now, the vector is formatted in descending order. This means the largest value will be the first one.

General Syntax: sort(vector-name.begin(),vector-name.end(),greater<int>());

Example:

```#include <bits/stdc++.h> using namespace std;

int main()

{

vector<int> zap{10, 80, 90, 46, 57, 30, 14, 222, 100}; //Declaration Of Vector

sort(zap.begin(), zap.end(), greater<int>()); // Using Sort Function For Descending Order

cout << "Descending Order: \n";

for (auto coding : zap) // Printing Vector

cout << coding << "-";

return 0;}```

Let us try to find out the output of the above code. It will help to find out the sort vector in C++ programming language.

Output: ## Conclusion:

As we saw it is very important to understand the sort vector in C++ programming language.

We need to understand how to sort a vector in CPP. This will help us a lot to solve difficult problems in the future.

It is advised to clear the basic concept of the C++ programming language. It will help to understand the sort vector in CPP in a very easy & clear manner.

So, hope you have liked this piece of article. Share your thoughts in the comments section and let us know if we can improve more.

Domycodinghomework also offers a wide range of programming and coding help services for you to benefit from. Don’t miss out! Visit https://domycodinghomework.com/ and our expert team will be there to help you out.