# Simple Bubble Sort swap counter C++

This is the solution for a simple challenge from Hackerrank.I am just trying to solve Sorting: Bubble Sort Problem on HackerRank.

We are asked to count the number of swaps performed during a bubble sort and to print the first and last item of the ordered vector.

### Sample input

3
3 2 1


First line is the number of elements in the vector, the second line is the vector

### Expected output

Array is sorted in 3 swaps.
First Element: 1
Last Element: 3


## Code

I've implemented my solution in C++ and would love to hear what your feedback and what could I have done cleaner

#include <iostream>
#include <vector>
#include <algorithm>

using big_int = long long;

std::size_t count;
big_int first_element;
big_int last_element;
};

template<class T>
std::size_t swap_count = 0;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v.size() - 1; j++) {
if (v[j] > v[j + 1]) {
std::swap(v[j], v[j + 1]);
swap_count++;
}
}
}
}

int main(int argc, char** argv) {
int n;
std::cin >> n;
std::vector<big_int> v(n);
for (int i=0; i<n; i++) {
std::cin >> v[i];
}

std::cout << "Array is sorted in " << answer.count         << " swaps.\n"
<< "First Element: "     << answer.first_element << "\n"
<< "Last Element: "      << answer.last_element;

}


– AEM
Apr 13, 2020 at 16:54
• @AhmedEmad I've updated it with the link Apr 13, 2020 at 16:57
• I saw and i have edited some changes and will support your question to find solution for it,Good Luck! @Blasco
– AEM
Apr 13, 2020 at 17:33

Your answer should also be a template struct. It contains first and last element of type big_int but in the bubble _sort function it is constructed with template argument type T.

template<class T>
std::size_t count;
T first_element;
T last_element;
};


But actually maybe the structure Is redundant because first And last element can Always easily be accessed inside the sorted container. Or it could at least contain just references to those elements instead of their copies in case the template argument T is something more complex then long long.

auto count = bubble_sort(v);
auto & first = v[0];
auto & last = v[v.size - 1];


Further instead of accepting vector<T>, you could accept two iterators of template argument type as Is common with many standard functions and allows to work not only for any type of container but also for just a range within a container.

template<class Iterator>
std::size_t bubble_sort(Iterator from, Iterator to);

// ...

auto count = bubble_sort(v.begin(), v.end());

• Nitpick: s/iterator/random access iterator; also s/container/container with random-access iterator Mar 24, 2020 at 1:25
• @L.F. good point, but I don't think we need to restrict to random access containers. I think any forward Iterator Is enough for the sorting itself. Random access Iterator Is only needed for constant time access to the first and last element. But actually some containers have constant access to first and last elements but not the other elements (like doubly linked list). Mar 24, 2020 at 8:29
• Oops, didn't realize bubble sort doesn't need random access. Sorry! I think forward iterator is enough then - since we are copying iterators and visiting the elements multiple times. Mar 24, 2020 at 8:30

Overall, your code looks great. It's nicely structured, and having a well-defined answer type makes immediately clear what the question is about.

At the bottom of the code, i=0 is missing some spaces.

You can improve the speed of the bubble sort by 50% by not counting j from 0 to size but only from 0 to size - 1 - i, since the last few elements are already bubbled up and therefore don't change anymore.

For testing, you should add another counter for the number of comparisons.

To test that your code is indeed correct, you should add an automatic test that takes a large array, fills it with 0 to size and shuffles the array. After sorting it should be the same as before.

I'm not sure whether the C++ compiler is smart enough to know that the vector's size doesn't change during the bubble sort. To get a simple performance boost, compute v.size() once and store it in a local variable. Measure the performance difference using a large array that is already sorted.

• Is it faster to cache the size of std::vector? The answers seem to suggest no. Mar 23, 2020 at 0:42
• From my understanding the compiler should optimize that, right? I've just checked, and with the default O0 it is true that one can see the size() calls. With O3 there isn't even a call to the function that I've defined bubble_sort(), I also see no calls to size() everything is optimized away Mar 23, 2020 at 11:54
• As I said, "I'm not sure". It's good that everything is optimized away, I wouldn't have expected this though since the compiler needs quite a lot of information to decide all this. Mar 23, 2020 at 20:58