Sorting a Vector in C++



Sorting a vector in C++ means arranging its elements in a specific order, like ascending or descending. This is a common task when you need to organize data efficiently. C++ provides different ways to sort a vector. In this article, we will look at different ways to sort a vector in C++. Let's look at this example to better understand it:

For the vector:
V = {5, 3, 8, 1, 2}
Sorted Output: {1, 2, 3, 5, 8}

For the vector:
V = {22, 23, 5, 6, 34}
Sorted Output: {5, 6, 22, 23, 34}

Approaches to Sort a Vector

We'll cover the following approaches to sort a vector in C++.

Using std::sort

The std::sort function is commonly used to sort a vector in C++. By default, it sorts the elements in ascending order, and it modifies the original vector.

Example

Here's the C++ code to sort a vector using std::sort. We call std::sort(v.begin(), v.end()) to sort the vector in ascending order.

#include <iostream>
#include <vector>
#include <algorithm>  // For std::sort
using namespace std;

int main() {
   vector<int> v = {5, 3, 8, 1, 2};
   cout << "Original vector: ";
   for (int i : v) {
      cout << i << " ";
   }
   cout << endl;
   // Sort the vector in ascending order
   sort(v.begin(), v.end());
   
   cout << "Sorted vector: ";
   for (int i : v) {
      cout << i << " ";
   }
   return 0;
}

Output

The output of the above program is as follows:

Original vector: 5 3 8 1 2 
Sorted vector: 1 2 3 5 8

Using std::stable_sort

The std::stable_sort function works like std::sort, but it keeps the order of elements that are equal. So, if two elements have the same value, their original order in the vector will stay the same.

Example

Here's the C++ code using std::stable_sort. It sorts the vector in ascending order while keeping the original order of equal elements unchanged.

#include <iostream>
#include <vector>
#include <algorithm>  // For std::stable_sort
using namespace std;

int main() {
   vector<int> v = {5, 3, 8, 1, 2, 5};
   cout <<"Original vector: ";
   for (int i : v) {
      cout << i << " ";
   }
   cout << endl;
   // Sort the vector using std::stable_sort
   stable_sort(v.begin(), v.end());
   
   cout <<"Sorted vector: ";
   for (int i : v) {
      cout << i << " ";
   }
   return 0;
}

Output

The output of the above program is as follows:

Original vector: 5 3 8 1 2 
Sorted vector: 1 2 3 5 8

Using a Custom Comparator

Sometimes, you may need to sort elements based on specific conditions, like sorting in descending order or sorting objects. You can do this by passing a custom comparator function to std::sort.

Example

Here's the C++ code to sort a vector of integers in descending order using a custom comparator. The comparator function returns true if the first element is greater than the second, which results in a descending order.

#include <iostream>
#include <vector>
#include <algorithm>  // For std::sort
using namespace std;

// Custom comparator function for descending order
bool compareDesc(int a, int b) {
   return a > b;
}

int main() {
   vector<int> v = {5, 3, 8, 1, 2};
   cout << "Original vector: ";
   for (int i : v) {
      cout << i << " ";
   }
   cout << endl;
   // Sorting in descending order using a custom comparator
   sort(v.begin(), v.end(), compareDesc);
   
   cout << "Sorted vector in descending order: ";
   for (int i : v) {
      cout << i << " ";
   }
   return 0;
}

Output

The output of the above program is as follows:

Original vector: 5 3 8 1 2 
Sorted vector in descending order: 8 5 3 2 1

Sorting in Descending Order Using Lambda Function

Instead of creating a separate custom comparator, you can use a lambda function directly in the std::sort call to sort in descending order.

Example

Here's how we can sort a vector in descending order using a lambda function.

#include <iostream>
#include <vector>
#include <algorithm>  // For std::sort
using namespace std;

int main() {
   vector<int> v = {5, 3, 8, 1, 2};
   cout << "Original vector: ";
   for (int i : v) {
      cout << i << " ";
   }
   cout << endl;
   // Sorting vector in descending order using a lambda function
   sort(v.begin(), v.end(), [](int a, int b) {
      return a > b;
   });
   cout << "Sorted vector in descending order: ";
   for (int i : v) {
      cout << i << " ";
   }
   return 0;
}

Output

The output of the above program is as follows:

Original vector: 5 3 8 1 2 
Sorted vector in descending order: 8 5 3 2 1

Using std::partial_sort

std::partial_sort is helpful when you only need to sort a portion of the vector, like the smallest or largest elements. It sorts the first n elements while leaving the rest of the vector in an undefined order.

Example

Here's the C++ code to partially sort a vector, keeping the smallest 3 elements at the beginning.

#include <iostream>
#include <vector>
#include <algorithm>  // For std::partial_sort
using namespace std;

int main() {
   vector<int> v = {5, 3, 8, 1, 2};
   cout << "Original vector: ";
   for (int i : v) {
      cout << i << " ";
   }
   cout << endl;
   // Partial sort the first 3 elements
   partial_sort(v.begin(), v.begin() + 3, v.end());
   cout << "Partially sorted vector: ";
   for (int i : v) {
      cout << i << " ";
   }
   return 0;
}

Output

The output of the above program is as follows:

Original vector: 5 3 8 1 2 
Partially sorted vector: 1 2 3 8 5

Conclusion

In this article, we looked at different ways to sort a vector in C++, like using std::sort, std::stable_sort, custom comparators, and sorting in descending order. We also covered std::partial_sort for sorting part of a vector. These methods help you pick the right approach for your needs in C++.

Updated on: 2025-01-30T14:46:25+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements