C++ 合并排序计数比较

问题描述

我正在从事一个项目,该项目要求我们实现不同的排序并添加计数器变量以测量具有不同数组大小的运行时。我的问题是我当前的输出与我的合并排序的预期输出不匹配。只差一点点。

错误有什么建议吗?

输出

Array Size:          10         100         1000         10000
--------------------------------------------------------------------
Merge sort           19          569         9275       129630

预期输出

Array Size:          10         100         1000         10000
--------------------------------------------------------------------
Merge Sort           19         550         8706         120355 

数组大小为 10 的数组内容是什么

Merge sort

[ 935,942,697,299,382,579,408,181,366,505 ] //unsorted
[ 181,505,935,942 ] //sorted

程序:

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>
      /******************************/
      /* Start of Merge Algorithm   */
      /******************************/

 /**
  * The maximum size of the temporary array
  */
const int MAX_SIZE = 10000;

int count = 0; //I added this global variable but It seems Incorrect

/**
 * Merges two sorted array segments theArray[first..mid] and
 *    theArray[mid+1..last] into one sorted array.
 */
template<class ItemType>
void merge(ItemType theArray[],int first,int mid,int last) {
    ItemType tempArray[MAX_SIZE];  // Temporary array
    // Initialize the local indices to indicate the subarrays
    int first1 = first;            // Beginning of first subarray
    int last1 = mid;               // End of first subarray
    int first2 = mid + 1;          // Beginning of second subarray
    int last2 = last;              // End of second subarray
    // While both subarrays are not empty,copy the
    // smaller item into the temporary array
    int index = first1;            // Next available location in tempArray
    while ((first1 <= last1) && (first2 <= last2)) {
        // At this point,tempArray[first..index-1] is in order
        if (theArray[first1] <= theArray[first2]) {
            tempArray[index] = theArray[first1];
            first1++;
            count++; //I incremented count here
        }
        else {
            tempArray[index] = theArray[first2];
            first2++;
            count++; //I incremented count here
        }  // end if
        index++;

    }  // end while

    // Finish off the first subarray,if necessary
    while (first1 <= last1) {
        // At this point,tempArray[first..index-1] is in order
        tempArray[index] = theArray[first1];
        first1++;
        index++;
    }  // end while

    // Finish off the second subarray,if necessary
    while (first2 <= last2) {
        // At this point,tempArray[first..index-1] is in order
        tempArray[index] = theArray[first2];
        first2++;
        index++;
    }  // end for

    // copy the result back into the original array
    for (index = first; index <= last; index++) {
        theArray[index] = tempArray[index];
    }
}

/**
 * Sorts the items in an array into ascending order.
 */
template<class ItemType>
int mergesort(ItemType theArray[],int last) {
    if (first < last) {
        // Sort each half
        int mid = first + (last - first) / 2;

        // Sort left half theArray[first..mid]
         mergesort(theArray,first,mid);

        // Sort right half theArray[mid+1..last]
         mergesort(theArray,mid + 1,last);
        // Merge the two halves
        merge(theArray,mid,last);
    }
    return count; //return count here
}


      /******************************/
      /* Start of Sorting Benchmark  */
      /******************************/
/**
 * Generate an array of random integers
 */
int* makeRandomArray(int n,int seed) {
    srand(seed);
    int * a = new int[n];
    for (int i = 0; i < n; i++) {
        a[i] = rand() % 1000;
    }
    return a;
}

int main(){
    const int seed = 9000;
    int *a;
    /******************************/
    /* Start of Merge Sort    */
    /******************************/
    std::cout << "Merge sort";

    int n = 10;
    a = makeRandomArray(10,seed);
    std::cout <<std::setw(13)<< mergesort(a,n-1);
    delete[] a;

    n = 100;
    a = makeRandomArray(100,n-1);
    delete[] a;

    n = 1000;
    a = makeRandomArray(1000,n-1);
    delete[] a;

    n = 10000;
    a = makeRandomArray(10000,n-1)<<std::endl;
    delete[] a;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)