1
\$\begingroup\$

For practice purpose, I wrote bubble sort in C++ two styles: C-ish and modern C++ compliant. I would like to have your comments on any point on these implementations.

Sorting.h
#ifndef SORTING_ALGORITHMS_H
#define SORTING_ALGORITHMS_H

#include <utility>
#include <vector>
//#include <iterator>
#include <algorithm>

namespace etpc
{
    template <class T>
    void sortBubble(T* pArrHead, std::size_t sArrSize)
    {
        for(std::size_t i=0; i<sArrSize-1; i++)
        {
            for(std::size_t j=0; j<sArrSize-i-1; j++)
            {
                if(pArrHead[j]>pArrHead[j+1])
                    std::swap(pArrHead[j], pArrHead[j+1]);
            }
        }
    }

    // May 2, 2020
    // Based on the following link
    // https://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort#C.2B.2B
    // typename vs class
    // https://stackoverflow.com/questions/2023977/difference-of-keywords-typename-and-class-in-templates
    template <class T>
    void sortBubble(std::vector<T>& vArrHead)
    {
        typename std::vector<T>::iterator begin = std::begin(vArrHead);
        typename std::vector<T>::iterator end = std::end(vArrHead);
        while (begin != end--) {
            for (auto it = begin; it != end; ++it) {
                if (*(it + 1) < *it) {
                    std::iter_swap(it, it + 1);
                }
            }
        }
    }
}
#endif // SORTING_ALGORITHMS_H
Main.cpp
#include "Sorting/include/Sorting.h"

#include <iostream>
#include <chrono>
#include <iterator>

int main()
{
    std::chrono::steady_clock::time_point begin;
    std::chrono::steady_clock::time_point end;
    std::ostream_iterator<int> out_it (std::cout,", ");

    static const int i32Size = 50;
    int arr[i32Size] = {7804, 50398, 14945, 1814, 51383, 63156, 8432, 58103, 28175, 4339, 8361, 37158, 1529, 43066, 62052, 9591, 13168, 332, 55913, 2418, 48066, 46504, 52922, 39523, 36653, 30402, 9373, 56202, 50539, 41187, 42606, 32278, 63902, 41668, 7505, 46534, 25846, 49739, 63411, 45933, 15042, 6544, 35718, 17035, 34647, 15212, 52690, 64299, 61535, 45071};
    begin = std::chrono::steady_clock::now();
    etpc::sortBubble<int>(arr, i32Size);
    end = std::chrono::steady_clock::now();
    std::cout << "Time difference impArray = " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << "[ns]" << std::endl;
    std::copy(std::begin(arr), std::end(arr), out_it);
    std::cout << '\n';

    std::vector<int> v = {7804, 50398, 14945, 1814, 51383, 63156, 8432, 58103, 28175, 4339, 8361, 37158, 1529, 43066, 62052, 9591, 13168, 332, 55913, 2418, 48066, 46504, 52922, 39523, 36653, 30402, 9373, 56202, 50539, 41187, 42606, 32278, 63902, 41668, 7505, 46534, 25846, 49739, 63411, 45933, 15042, 6544, 35718, 17035, 34647, 15212, 52690, 64299, 61535, 45071};
    begin = std::chrono::steady_clock::now();
    etpc::sortBubble<int>(v);
    end = std::chrono::steady_clock::now();
    std::cout << "Time difference impVector = " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << "[ns]" << std::endl;
    std::copy(std::begin(v), std::end(v), out_it);
    std::cout << '\n';

    return 0;
}
```
\$\endgroup\$
3
  • \$\begingroup\$ You can still replace it +1 by std::next(it) \$\endgroup\$
    – JVApen
    May 2, 2020 at 20:29
  • \$\begingroup\$ It's unclear to me what you are trying to get out of this review, can you elaborate? \$\endgroup\$
    – JVApen
    May 2, 2020 at 20:34
  • \$\begingroup\$ For C-style implementation, coding practises, algorithm-specific recommendation or your personal comments. For modern C++ implementation, does it suit with modern C++, can it be improved etc? @JVApen \$\endgroup\$
    – Erdem Tuna
    May 3, 2020 at 11:16

1 Answer 1

5
\$\begingroup\$

As you're already working with iterators you might as well accept iterators as arguments instead of the std::vector (as all STL algorithms do). That way it will work with other containers as well, even with raw pointers.

The comparison can also be customized like in std::sort. Maybe the caller wants to sort in descending order or compare structures by some field.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.