如何解决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 举报,一经查实,本站将立刻删除。