简体   繁体   中英

Mergesort Swaps and Comparisons

I'm currently working on an analysis project where I'm observing how different algorithms behave when implemented in Java. I got some code which implements a Mergesort algorithm from online, now I need to run this code on an array of 10,000 randomly generated integers (between 1 and 100,000) and record how many swaps and comparisons were made.

I'm not exactly sure at which point in the code to increment the variables that count Swaps and Comparisons. What would the expected value be? Since best, worst, and average case for Mergesort are all nlog(n) does this mean I should expect 10,000*(log base 2 of 10,000) approx = 138,000 for the sum of swaps and comparisons?

Here is the code, I'm guessing that a swap only happens when the original array is altered, comparisons I'm not too sure about:

void MergeSort(int low, int high)
   // a[low : high] is a global array to be sorted.
// Small(P) is true if there is only one element to
// sort. In this case the list is already sorted.
{
   if (low < high) { // If there are more than one element
          // Divide P into subproblems.
          // Find where to split the set.
          int mid = (low + high)/2;
          // Solve the subproblems.
          MergeSort(low, mid);
          MergeSort(mid + 1, high);
          // Combine the solutions.
          Merge(low, mid, high);
   }
}

   void Merge(int low, int mid, int high)
 // a[low:high] is a global array containing two sorted
 // subsets in a[low:mid] and in a[mid+1:high]. The goal
 // is to merge these two sets into a single set residing
 // in a[low:high]. b[] is an auxiliary global array.
 {
     int h = low, i = low, j = mid+1, k;
   while ((h <= mid) && (j <= high)) {
      if (a[h] <= a[j]) { b[i] = a[h]; h++; }
      else { b[i] = a[j]; j++; } i++;
   }
   if (h > mid) for (k=j; k<=high; k++) {
                   b[i] = a[k]; i++;
                }
   else for (k=h; k<=mid; k++) {
           b[i] = a[k]; i++;
        }
   for (k=low; k<=high; k++) a[k] = b[k];

}

I'm not exactly sure at which point in the code to increment the variables that count Swaps and Comparisons.

I suggest you create helper methods for the swap and the compare operation. That would give you good places for the increment-counter code.

Since best, worst, and average case for Mergesort are all nlog(n) does this mean I should expect 10,000 (log base 2 of 10,000) approx = 138,000 for the sum of swaps and comparisons?*

What you can expect is that the number of comparisons is proportional to n log(n) where the size of the input is n .

In Your Merge Function i added a variable count which will have the number of total swaps done

  while ((h <= mid) && (j <= high)) {
      if (a[h] <= a[j]) { b[i] = a[h]; h++; }
      else { b[i] = a[j]; j++; count+=mid-h+1; } i++;
  }

I'm actually doing this for Homework in Algorithms and Data Structures. The thread is a bit dusty but for anyone who could use it this is what I got:

In your Merge method

while ((h <= mid) && (j <= high)) {
  if (a[h] <= a[j]) { b[i] = a[h]; h++; }
  else { b[i] = a[j]; j++; } i++;
}

the if statement is where the comparison is being made, I almost want to say that even if you make it to the else statement a comparison is made as well due to the if statement failing.

The else statement is where the swap starts to be made, if you put a counter in the else statement it will count up all the swaps. I confirmed this by checking the array twice, once when unsorted and again when sorted. I'm not 100% on this so any feedback is appreciated. It's a little easier to see in my assignment because I'm sorting strings, these are the same lines in your Merge function posted above, from my assignment:

while(leftPos<=leftEnd && rightPos<=rightEnd)
{
    mergeSortComparisons++;

    if (a[leftPos].compareTo(a[rightPos]) <= 0)     
        tmpArray[tmpPos++]=a[leftPos++];

    else 
    {
        tmpArray[tmpPos++]=a[rightPos++];
        mergeSortSwaps++;
    }
}

mergeSortSwaps and mergeSortComparisons are class variables that are set in the constructor. I can reset them if I recall the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM