-2

Hello I am doing project where I have different sorting algorithms (QuickSort, BubbleSort etc). I measure time of each of them and at the end I need to make some comparison which one is the fastest, and so on.

bool testyBubblesort(){
   long int before = GetTickCount();
   a.BubbleSort_int();
   long int after = GetTickCount();
   double dif = double((after - before));
   result &= testEqual(zoradane.vypis_pola(), a.vypis_pola(),dif, "Test sort array-int");
   comparison(dif);
   vypisVysledkuTestu(result, "Testy triedenia BubbleSortom");   
   return result;
}

This is my function, it tests whether arrays are sorted and measures time. The important part is comparison(dif); This should be the function that compares all sorts. Note that i use this function in other sorts too.

void comparison(double time){
    vector<int> all_times;
    all_times.push_back(time);
}

So i was thinking make vector array and then sort it is good idea but I dont know how I always get some error. Can u help me ? Thanks

Xarn
  • 3,021
  • 20
  • 40
  • 3
    What error? Note that you are recreating a new vector `all_times` on each call to `comparision`: the error is related to that, most probably. – Stefano Sanfilippo Feb 08 '14 at 14:56
  • I am with Stefano here, that looks like the worst offender. Also, you are sending in time as a `double`, why do you have that vector as `int`? – Xarn Feb 08 '14 at 14:57
  • yes there is problem probably here. But how can I make to save all the times somewhere and the compare it with some function ? – user3287392 Feb 08 '14 at 15:03
  • @user3287392 The easiest and most reliable way to test performance of different algos is using a profiler (see e.g. ['What can I use to profile C++ code in Linux?'](http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-code-in-linux), [shinyprofiler](https://code.google.com/p/shinyprofiler/)). – πάντα ῥεῖ Feb 08 '14 at 15:24
  • all_times should not be a local variable. Also it should not be a vector of integers. – drescherjm Feb 08 '14 at 15:42

1 Answers1

1

You aren't comparing to anything because your vector only has one element.

You should replace "comparison" with just an insertion into a vector that is declared globally. Then, at the top-level (wherever calls testyBubblesort), and perform your comparisons there.

A more robust method would be to make a vector, where MyStruct is declared as a double and a string, so that you store {time, "Bubble Sort"} (allowing you to associate the runtime with the sorting algorithm used. Then at the top-level, just use the built-in sort function (you will have to use the version that accepts a function to define how to order MyStruct) on the vector and grab the first object from the vector. Then just print out the string.

dvntehn00bz
  • 527
  • 5
  • 10