-1

I have 4 variables Quick, Bubble, Insert and Select with measured time and I want make some results comparison which looks like this

cout << "1st  QuickSort with time " << quick_time << " ms" << endl; 
cout << "2st  InsertSort with time " << insert_time << " ms" << endl; 

However making comparisons with if for 4 variables seem to be very long and complicated but when I try to insert these variables into array and sort it I loose information about which sort is on which position.

How can I make this with nice and not messy code most efectively ? Thanks

user3292179
  • 55
  • 1
  • 6
  • Comparing with `if` takes not so much time comparing with sort time, so you should worry about efficiency in this place.. – ST3 Feb 10 '14 at 14:16

1 Answers1

0

You can use a more structured representation to keep the information about which sort you have, for instance a std::pair, then sort it:

using namespace std;
typedef pair<float, string> tTimeRecord;
vector<tTimeRecord> records;

//populate records
records.emplace_back(quick_time, "QuickSort");
...

//sort
sort(begin(records), end(records));

//output
size_t idx = 0;
for (const auto& record : records) {
    ++idx;
    cout << "#" << idx << ": " << record.second << " with time: " << record.first << endl;
}

using a std::pair has the nice property that sorting uses the standard comparison for pairs (lexicographical comparison), so you don't have to define a comparator yourself.

Martin J.
  • 5,028
  • 4
  • 24
  • 41