Open In App

Sorting a Vector in C++

Last Updated : 13 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Sorting a vector means arranging the elements of vector in ascending order or in descending order or in desired defined order. In this article, we will learn about different ways to sort the vector in C++.

The most efficient way to sort the vector is by using sort() method. Let’s take a look at an example to sort the vector of integers in descending order:

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

// Comparator function
bool comp(int a, int b) {
    return a > b;
}

int main() {

    vector<int> v = {1, 4, 3, 2, 5};

    // Sort vector in descending order
    sort(v.begin(), v.end(), comp);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
5 4 3 2 1 

Explanation: Here we sort the vector in descending order using custom comparator function. If we want to sort the vector in ascending order, then we do not even require the custom comparator as sort() function sorts the given range in ascending order by default.

There are also some other methods in C++ by which we can sort the vector. Some of them are as follows:

Using stable_sort()

The stable_sort() method is similar to sort() method, but the only difference is that the stable_sort() maintains the order of elements if the elements are equal.

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

int main()
{
    vector<int> v = {1, 4, 3, 2, 5};

    // Sort vector in ascending order
    stable_sort(v.begin(), v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Explanation: Here we sort the vector in ascending order using stable_sort() method which maintains the order of elements.

Using Multiset

Multiset is an ordered container that stores the data in the given sorted order. It can be used to sort vector by first pushing all the elements from vector to multiset and then putting them back one by one.

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

int main()
{
    vector<int> v = {1, 4, 3, 2, 5};

    // Create multiset from v
    multiset<int> ms(v.begin(), v.end());

    // Copy back all the elements frmo ms to v
    v.assign(ms.begin(), ms.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 

Explanation: The multiset container stores elements in ascending order by default, but it can be changed by providing a comparator while declaring the multset.

If you want to implement any other sorting algorithm such as bubble sort, counting sort, etc. you will have to implement them by yourself.

Using Bubble Sort Algorithm

Bubble Sort is a simple sorting algorithm that repeatedly swap adjacent elements if they are in the wrong order. This process is repeated until the vector is sorted.

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

// Bubble sort implementation
void bubbleSort(vector<int> &v)
{
    int n = v.size();
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (v[j] > v[j + 1])
                swap(v[j], v[j + 1]);
        }
    }
}

int main()
{
    vector<int> v = {1, 4, 3, 2, 5};

    bubbleSort(v);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 


Next Article
Article Tags :
Practice Tags :

Similar Reads