1

I am writing a program that calculates the time it takes to sort a vector.
There's multiple sorters and I want to be able to call them all from the same function.
Here's on of the sorter itself.
Please note that the sorter has to be generic.

SelectionSort.h

template <typename T>
void selectionSort(T& v){
   if(!v.empty()){
      size_t iMin;
      for(size_t i = 0; i < v.size(); ++i){
         iMin = i;
         for(size_t j = i + 1; j < v.size();++j){
            if (v[j] < v[iMin])
               iMin = j;
         }
         std::swap(v[i], v[iMin]);
      }
   }
}

Here's the part where I calculate the time it takes to sort.
In that case I want it to be a vector of int.

timeCalculation.cpp

typedef std::vector<int> V;
template <typename Fn>
   std::chrono::duration<double, std::milli>
   mesureTemps(Fn f,V &v1, unsigned iter) {
      std::chrono::duration<double, std::milli> tempsTot{};
      for(unsigned i = 0; i < iter; ++i) {
         std::shuffle(v1.begin(),v1.end(),std::rand());
         auto t1 = std::chrono::high_resolution_clock::now();
         f(v1);
         auto t2 = std::chrono::high_resolution_clock::now();
         tempsTot += (t2 - t1);
      }
   return tempsTot /= iter;
}
int main() {
   std::vector<int> v(N);
   std::iota(v.begin(),v.end(),1);
   auto tempsMoyen = mesureTemps(selectionSort, v,
      10);
   std::cout << tempsMoyen.count();
   return EXIT_SUCCESS;
}

I cannot find a way to call mesureTemps without breaking at compilation.

Addrar
  • 11
  • 1
  • Don't use `high_resolution_clock` for timing, it's not guaranteed to be monotonic. Use `steady_clock` instead. – chris Feb 26 '22 at 21:22
  • 1
    `selectionSort` in the context you're using it in `main` is broken because you never tell it the type of container it's using. `selectionSort` *should* work in that context, though I'm gonna be honest with you, this is much easier with iterators all-around rather than pumping your container type down the template argument chute. Anyway, that `std::rand()` is clearly wrong as well. – WhozCraig Feb 26 '22 at 21:29
  • 1
    Offtopic but important: don't call `std::swap` directly, use `using std::swap; swap(v[i], v[iMin]);` instead. – n. m. could be an AI Feb 26 '22 at 21:31
  • @n.1.8e9-where's-my-sharem. Why is that important? I've not seen it done elsewhere. – Paul Sanders Feb 26 '22 at 22:27
  • @PaulSanders https://stackoverflow.com/questions/28130671/how-does-using-stdswap-enable-adl – n. m. could be an AI Feb 26 '22 at 22:54
  • `mesureTemps(selectionSort, v, 10);` – Igor Tandetnik Feb 27 '22 at 01:53
  • @IgorTandetnik this creates another problem at compilation , ```error: 'std::remove_reference::type' {aka 'int'} is not a class, struct, or union type``` – Addrar Feb 27 '22 at 10:41
  • `std::shuffle(v1.begin(),v1.end(),std::rand())` call is wrong. You are passing an `int` where a [*UniformRandomBitGenerator*](https://en.cppreference.com/w/cpp/named_req/UniformRandomBitGenerator) object is expected. – Igor Tandetnik Feb 27 '22 at 13:41

0 Answers0