Posting useful tips and guides for programming.

Follow

# How to Bubble Sort a Vector in C++?

So recently, you’ve started learning data structures and wanted to know how to sort a vector using bubble sort in C++ (CPP). 🤔

The most effective technique to handle massive data is to store it optimally. It is relatively simple to answer this problem in the shortest time if your data is structured in a specific format, such as either an ascending or a descending format since programmers frequently have many challenges to handle daily. To arrange your data in a specific format and sort them in an ascending or descending order, every programming language offers a family of algorithms known as sorting.

Several sorting methods may be employed in various contexts. In this post, we’ll examine bubble sort, one of these sorting algorithms. Furthermore, we’ll discuss vectors using Bubble sort in C++.

We’ll discuss bubble sort vectors in C++.

## What is Bubble Sort in C++?

The Bubble Sort, also known as Sinking Sort, is a sorting algorithm that evaluates each pair of neighboring components. Although bubble sort is not the most effective sorting algorithm when compared to others, it offers novices helpful information on what a sorting algorithm is and how it functions in practice.

The first member of the list is compared to the second, the second to the third element, and so on. This is the fundamental approach to bubble sorting.

Similar to how air bubbles rise to the surface of the water, each iteration pushes each array member closer to the conclusion. This is how the name “bubble sort” came about. Sorting is carried out in this instance, bypasses or iterations. Consequently, after each iteration, the most prominent member is positioned where it belongs in the list. You might also argue that the item with the most significant size comes to the top of the list after each iteration or pass.

You should use a manageable data set with this approach due to its high average and worst-case time complexity.

## Where is Bubble Sort Used?

Bubble sort is frequently used to illustrate the idea of a sorting algorithm because of how straightforward it is. It is well-known in computer graphics for its ability to find minimal fault (such as a swap of just two components) and correct it with linear complexity (2n).

For instance, in a polygon filling method, bounding lines are ordered by their x-coordinate at a particular scan line (parallel to the x-axis). With incrementing y, only crossings of two lines will cause their order to change (two elements to be swapped).

## What Are Vectors in C++?

Vectors have the same properties as dynamic arrays, including the ability to automatically resize when an element is added or removed and automated storage management by the container. Iterators can access and navigate vector items since they are stored in contiguous storage. Data is added at the end of vectors. Inserting at the end requires more time since the array may occasionally need to be expanded.

## How to Sort a Vector Using Bubble Sort in C++?

We implement bubble sort in an example program to operate on generic vector objects. The entire sorting process may be defined by the single-function Bubble Sort. The single argument for the templated function is a reference to a vector.

To cycle over the vector items until they are sorted in ascending order, Bubble Sort uses two for loops and one outer while loop. You’ll see that we streamlined the implementation and improved readability by using the std::swap technique.

Bubble sort vectors C++ means sorting a vector through bubble sort. Let’s see an example:

Code

```#include <iostream>

#include <vector>

using namespace std;

void BubbleSort(vector<int>& a);

void print_vector(vector<int> a);

int main(int argc, char const *argv[])

{

vector<int> a {2,9,0,4,5,1,8,7};

printf("Vector before sorting : ");

print_vector(a);

printf("Vector after sorting : ");

BubbleSort(a);

print_vector(a);

}

void BubbleSort(vector<int>& a)

{

bool swap = true;

while(swap){

swap = false;

for (size_t i = 0; i < a.size()-1; i++) {

if (a[i]>a[i+1] ){

a[i] += a[i+1];

a[i+1] = a[i] - a[i+1];

a[i] -=a[i+1];

swap = true;
}
}
}
}

void print_vector(vector<int> a){

for (size_t i=0;  i <a.size();  i++) {

cout<<a[i]<<" ";
}

cout<<endl;

}```

Output

```Vector before sorting: 2 9 0 4 5 1 8 7

Vector after sorting: 0 1 2 4 5 7 8 9```

In the above example, you can see that we have declared a vector{2,9,0,4,5,1,8,7} which is an unsorted vector; you can use Bubble sort here to sort the vector. A sorted vector {0 1 2 4 5 7 8 9 } is printed in the output.

#### Conclusion

In the above article, we have discussed Bubble sort vector in C++ and the Advantages and Disadvantages of Bubble sort. Then we discussed Vectors in C++ and How to sort a vector using Bubble sort in C++.

Furthermore, we have discussed the complexity and simplicity of the bubble sort and where the Bubble sort can be used. Let’s have a quick recap of the topics discussed in this article.

1. What is Bubble sort?
2. Where is Bubble sort used?
3. What are vectors in C++?
4. Bubble sort vectors in  c++?

If you’ve found this article helpful, comment below and let 👇 know which solutions have helped you solve the problem.

## How to Round to Two Decimal Places in C++?

Do you want to learn more about rounding floating point numbers in CPP (C++) and how to round…

## How to Create a Destructor for Linked List in C++?

Are you getting trouble creating a “destructor for linked list in C++” and need to know how to…