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;
}
```
asked May 2, 2020 at 8:44
\$\endgroup\$
3
  • \$\begingroup\$ You can still replace it +1 by std::next(it) \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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.

answered May 2, 2020 at 10:03
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.