Reputation: 214
I need to count how many swaps and comparisons happened during merge sorting, I think my comparison number counting is fine, just due recursion I get as much numbers as my array length, somehow i need to store those numbers to variable in merge sort function?Also would love to know ideas on how to count total swaps
void merge(double arr[], int l, int m, int r)
{
int counter = 0;//number of comparisons
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int cc;
/* create temp arrays */
double L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
counter++;
}
cout << counter << endl;
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(double arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
Upvotes: 1
Views: 6697
Reputation: 35454
This is similar to the answer given by @JerryCoffin, but maybe a little bit simpler for you to digest.
A simple solution is to place this code in a class and have a member variable, maybe called int swapcount
, that will increment for each swap done. This alleviates having to maintain a local swap counter variable.
class MergeSorter
{
int swapCount;
void merge(double arr[], int l, int m, int r);
void mergeSort(double arr[], int l, int r);
public:
MergeSorter() : swapCount(0) { }
void startSort(double arr[], int l, int r);
int getSwapCount() const { return swapCount; }
};
void MergeSorter::merge(double arr[], int l, int m, int r)
{
// code here to merge. In here, you increment swapCount
}
void MergeSorter::mergeSort(double arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void MergeSorter::startSort(double arr[], int l, int r)
{
swapCount = 0; // start at 0
mergeSort(arr, l, r);
}
int main()
{
double arr[] = {1, 2, 3, 45, 5, 7};
MergeSorter ms;
ms.startSort(arr, 0, 5); // or whatever the arguments are
std::cout << "The number of swaps is " << ms.getSwapCount();
}
Note that we have a function startSort
that starts the sort within the class. In the merge
function, the swapCount
is incremented as needed. At the end, we just output the value of ms.swapCount
.
Upvotes: 1
Reputation: 184
If I understand correctly a swap happens when the original array is altered in merging steps. So in each call of merge I look at the value in original index and compared it with the newly formed value corresponding to that index. If they are different I incremented swap.
int swap=0;
int index(std::vector<double> v,double val){
auto match = std::find(v.begin(), v.end(), val);
return match-v.begin();
}
std::vector<double> merge(std::vector<double> v1,std::vector<double> v2,std::vector<double>& v,int start){
auto it1=std::begin(v1);
auto it2=std::begin(v2);
std::vector<double> newvec;
while(it1!=v1.end() && it2!=v2.end()){
if(*it1<=*it2){
newvec.push_back(*it1);
const FloatingPoint<double> l1(*it1), r1(v.at(index(newvec,*it1)+start));
if (!l1.AlmostEquals(r1)) swap++;
it1++;
}
else{
newvec.push_back(*it2);
const FloatingPoint<double> l2(*it2), r2(v.at(index(newvec,*it2)+start));
if (!l1.AlmostEquals(r1)) swap++;
it2++;
}
}
while(it1!=v1.end()){
newvec.push_back(*(it1++));
const FloatingPoint<double> l3(*it1), r3(v.at(index(newvec,*it1)+start));
if (!l3.AlmostEquals(r3)) swap++;
}
while(it2!=v2.end()){
newvec.push_back(*(it2++));
const FloatingPoint<double> l4(*it2), r4(v.at(index(newvec,*it2)+start));
if (!l4.AlmostEquals(r4)) swap++;
}
return newvec;
}
std::vector<double> merge_sort(std::vector<double> v,int start){ // start=0 when it is first called
int size=v.size();
if(size==1) return v;
std::vector<double> merged1,merged2;
auto it=v.begin();
merged1.assign(it,it+(size+1)/2);
merged2.assign(it+(size+1)/2,v.end());
std::vector<double> v1=merge_sort(merged1,start);
std::vector<double> v2=merge_sort(merged2,start+(size+1)/2);
v=merge(v1,v2,v,start);
return v;
}
Upvotes: 1
Reputation: 490408
I would do the job rather differently.
Instead of trying to instrument the sort itself to track the number or comparisons and swaps, I'd create a type that keeps track of the number of times it's compared and/or swapped. I was too lazy to write a whole merge sort to demonstrate it, but here's one doing a bubble sort:
#include <algorithm>
#include <iostream>
#include <vector>
namespace instrumented {
template <class T>
class wrapper {
T value;
public:
static std::size_t comparisons;
static std::size_t swaps;
wrapper(T val) : value(val) {}
bool operator<(wrapper const &other) { ++comparisons; return value < other.value; }
operator T() const { return value; }
static void reset() { comparisons = swaps = 0; }
static std::ostream &report(std::ostream &os) {
os << "comparisons: " << comparisons << "\n";
os << "swaps: " << swaps << "\n";
comparisons = 0;
swaps = 0;
return os;
}
};
template <class T>
std::size_t wrapper<T>::comparisons;
template <class T>
std::size_t wrapper<T>::swaps;
template <class T>
void swap(wrapper<T> &a, wrapper<T> &b) {
++wrapper<T>::swaps;
auto temp{ a };
a = b;
b = temp;
}
}
template <class T>
void sort(std::vector<T> &input) {
std::vector<instrumented::wrapper<T>> values;
for (auto const & i : input)
values.push_back(i);
for (auto i = 0; i < values.size() - 1; i++)
for (auto j = i + 1; j < values.size(); j++)
if (values[j] < values[i])
swap(values[i], values[j]);
values[0].report(std::cout);
}
int main() {
std::vector<int> inputs1{ 5, 4, 3, 2, 1 };
sort(inputs1);
std::cout << "\n";
std::vector<int> inputs2{ 10, 9, 7, 8, 5, 100, 2, 3, 1, 17, 6 };
sort(inputs2);
}
Note that with this, you should be able to (for example) get results for std::sort
, std::partial_sort
, and so on, not just your own sort functions.
Upvotes: 2