How to Sort Vector in Ascending Order in C++?
Last Updated :
25 Nov, 2024
Sort a vector in ascending order means arranging the elements of vector in such a way that the first element will be smallest, second element will be second smallest and so on. In this article, we will learn different ways to sort the vector in ascending order in C++.
The most efficient way to sort the vector in ascending order is by using sort() method. Let’s take a look at a simple example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 4, 3, 2, 5};
// Sort the vector
sort(v.begin(), v.end());
for (auto i : v)
cout << i << " ";
return 0;
}
There are also some other methods in C++ by which we can sort the vector in ascending order. Some of them are as follows:
Using stable_sort()
The stable_sort() method is similar to sort() method with the only difference being 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;
}
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;
}
Explanation: The multiset container stores elements in ascending order by default.
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};
// Sort v usign bubble sort
bubbleSort(v);
for (auto i : v)
cout << i << " ";
return 0;
}
Similar Reads
Sort Vector of Pairs in Ascending Order in C++ Sorting a vector of pairs in ascending order means arranging the elements in such a way that first pair is lesser than second pair, second pair is lesser than third pair and so on.The easiest way to sort the vector of pairs in ascending order is to use std::sort() function. The below code example il
4 min read
How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S
2 min read
How to Sort an Array in Descending Order using STL in C++? Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn how to sort an array in descending order using STL in C++. ExamplesInput: arr[] = {11, 9, 45, 21};Output: 78
4 min read
How to Insert an Element in a Sorted Vector in C++? In C++, inserting element in a sorted vector should be done such that it preserves the order of elements. In this article, we will learn different methods to insert an element in a sorted vector in C++.The recommended way to insert an element is to first find the position of insertion using lower_bo
4 min read
How to Find the Median of Sorted Vector in C++? In C++, vectors are dynamic arrays and the median is a middle value when data is sorted in ascending order. In this article, we will learn how to find the median of all elements in a sorted vector in C++. Example Input: myVector = {10, 20, 30, 40, 50}; Output: Median is 30Finding Median of Sorted Ve
2 min read