Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:40:32+00:00 2026-06-15T08:40:32+00:00

I would like to add compare++ to this code in order to count how

  • 0

I would like to add compare++ to this code in order to count how many compares are done by this algorithm. Does the count of compare need to be incremented on each execution of the first while loop in Merge(...) and in the if and else inside the while? Are these the only locations where compare should be incremented? (I added this increment where I thought it belonged and commented out. Please ignore the swap function)

#include "MergeSort.h"

template<class ItemType>
void MergeClass<ItemType>::sort(ItemType values[], int first, int last)
// Post: The elements in values are sorted by key.
{
    if (first < last)
    {
        int middle = (first + last) / 2;
        sort(values, first, middle);
        sort(values, middle + 1, last);
        Merge(values, first, middle, middle + 1, last);
    }
}

template<class ItemType>
void MergeClass<ItemType>::Merge(ItemType values[], int leftFirst, int leftLast,
int rightFirst, int rightLast)
// Post: values[leftFirst]..values[leftLast] and
// values[rightFirst]..values[rightLast] have been merged.
// values[leftFirst]..values[rightLast] are now sorted.
{
    ItemType tempArray[5];
    int index = leftFirst;
    int saveFirst = leftFirst;
    while ((leftFirst <= leftLast) && (rightFirst <= rightLast))
    {
        if (values[leftFirst] < values[rightFirst])
        {
            tempArray[index] = values[leftFirst];
            leftFirst++;
            //compare++;
        }
        else
        {
            tempArray[index] = values[rightFirst];
            rightFirst++;
            //compare++;
        }
        index++;
        //compare++;
    }
    while (leftFirst <= leftLast)
    // Copy remaining items from left half.
    {
        tempArray[index] = values[leftFirst];
        leftFirst++;
        index++;
    }
    while (rightFirst <= rightLast)
    // Copy remaining items from right half.
    {
        tempArray[index] = values[rightFirst];
        rightFirst++;
        index++;
    }
    for (index = saveFirst; index <= rightLast; index++)
        values[index] = tempArray[index];
}


template<class ItemType>
inline void MergeClass<ItemType>::Swap(ItemType& item1, ItemType& item2)
// Post: Contents of item1 and item2 have been swapped.
{
    ItemType tempItem;
    tempItem = item1;
    item1 = item2;
    item2 = tempItem;
}

template<class ItemType>
MergeClass<ItemType>::MergeClass()
{
    compare = 0;
    swap = 0;
}
template<class ItemType>
void MergeClass<ItemType>::sortPreformance()
{
    cout << "Comparisons made: " << compare <<endl;
    cout << "Swaps made: "<< swap <<endl;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:40:33+00:00Added an answer on June 15, 2026 at 8:40 am

    If it’s meant strictly for profiling, I’d put the counting logic outside of the sorting class. That is something like the following (which just counts the number of comparisons and swaps used by std::sort):

    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    using namespace std;
    
    template<typename T>
    struct CountingItem {
        CountingItem(const T& val = T()) : val_(val) {}
    
        bool operator<(const CountingItem<T>& rhs) const {
            ++compares;
            return val_ < rhs.val_;
        }
    
        static size_t compares;
        static size_t swaps;
        T val_;
    };
    
    template<typename T>
    size_t CountingItem<T>::compares = 0;
    template<typename T>
    size_t CountingItem<T>::swaps = 0;
    
    template<typename T>
    void swap(CountingItem<T>& a, CountingItem<T>& b) {
        ++CountingItem<T>::swaps;
        std::swap(a, b);
    }
    
    int main()
    {
        const size_t num_items = 10000;
        CountingItem<int> items[num_items];
        for(int i = 0; i < num_items; i++) items[i] = rand() % 100;
        sort(items, items+num_items);
        cout << "Compares = " << CountingItem<int>::compares << endl;
        cout << "Swaps    = " << CountingItem<int>::swaps << endl;
    
        // Reset CountingItem<int>::compares and swaps here if you're running another test
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have own project and i would like add for this class same as
I would like to add a small dice-rolling effect to my Javascript code. I
i just read this code example: http://robaustin.wikidot.com/how-does-the-performance-of-arraylist-compare-to-array what causing j = INT_ARRAY[i]; to be
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an
I would like to add a custom calculation method to a managed object (which
I would like to add roots to a VirtualTreeView http://www.delphi-gems.com/index.php/controls/virtual-treeview with a thread like
I would like to add a hash into an array using Ruby version 1.8.7:
I would like to add up the number of line changes in an SVN
I would like to add a pop effect when I hover over a SVG

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.