合并排序问题,在方法之间传递数组
Merge Sort Issue, passing array between methods?
我正在编写一个实现QuickSort,插入排序和合并排序的程序。除了合并排序外,我可以让所有工作都可以工作,而且我不知道为什么。请忽略操作和比较变量。它们是这样,我可以分析不同输入文件的运行时和操作数量。此代码以这种方式从主要对象运行:
Merge testobj13(test_Med[0], 100);
testobj13.merCall();
testobj13.display();
对于排序列表,该类将列表保留为排序,对于反向列表,列表仍然几乎保持相反,除了第一个和最后一个值,对于随机列表,我看不到输出和原始输入之间的任何模式。在等待答案时,我将尝试回答其他问题。即使只是我的代码或语法与此处的整体问题无关,也欢迎任何批评。我根据我的算法类撰写的sudo代码编写了此代码,所以我在这里找不到问题。
# include <iostream>
# include <stdio.h>
# include <stdlib.h>
using namespace std;
class Merge{
public:
int comparisons, operations, middle, i, j, k, size;
int *myArray, *c;
Merge::~Merge(){
myArray = 0;
delete myArray;
}
Merge::Merge(int a [], int n) {
size= n;
myArray= new int[size];
comparisons = 0, operations = 0, middle = 0, i = 0, j = 0, k = 0;
for(int x = 0; x < size; x++){
myArray[x] = a[x];
}
}
void combine(int arr [], int first, int middle, int last){
i = first, j = middle + 1, k = first; operations = operations + 3;
c = new int[last + 1]; operations ++;
while( (i <= middle) && (j <= last) ){
comparisons++;operations++;
if(arr[i] <= arr[j]){operations++;
c[k] = arr[i]; operations++;
i++; operations++;
}
else{
c[k] = arr[j]; operations++;
j++; operations++;
}
k++; operations++;
}
while(i <= middle){operations++;
c[k] = arr[i]; operations++;
i++; operations++;
k++; operations++;
}
while(j <= last){operations++;
c[k] = arr[j]; operations++;
j++; operations++;
k++; operations++;
}
for(int k = first; k <= last; k++){operations++;
arr[k] = c[k]; operations++;
}
c = 0;
delete c;
}
void mer(int arr [], int first, int last){
operations++; //for the comparison in the following if statement
if ( first < last ){
middle = (first + last) / 2; operations++;
mer(arr, first, middle); operations++;
mer(arr, middle + 1, last); operations++;
combine(arr, first, middle, last); operations++;
}
}
void merCall(){
mer(myArray, 0, size - 1);
}
void display(){
cout << "The array after going through Merge Sort: " ;
for(int x = 0; x < size; x++){
cout << endl << myArray[x];
}
cout << endl << "Number of operations :" << operations << "t comparisons: " << comparisons << endl;
}
};
您的"中间"变量在递归过程中被覆盖,因为它是类成员而不是本地变量:
:middle = (first + last) / 2; operations++;
// This is going to affect middle
mer(arr, first, middle); operations++;
// So this isn't going to work on the range you think it is.
mer(arr, middle + 1, last); operations++;
combine(arr, first, middle, last); operations++;
最好将中间声明为局部变量:
int middle = (first + last) / 2; operations++;
mer(arr, first, middle); operations++;
mer(arr, middle + 1, last); operations++;
combine(arr, first, middle, last); operations++;
相关文章:
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 介于 [固定数组] 和 [带内存分配的指针] 之间的性能
- 动态分配的数组和静态数组之间的区别
- 删除[i] 数组和删除数组 [i] 之间的区别
- 当数组位于两个循环之间时,您可以调用数组的 void 函数吗?
- 处理数组时make_unique和make_shared之间的差异
- 在两个结构之间共享数组
- C++堆栈中数组之间的间隙
- 类型测试对象的动态数组的这两个声明之间的区别?
- 如何计算大小为 1000 x 1000 的 2D 数组中 2 个元素之间的步幅?C++
- sizeof(空结构)和sizeof(带有空数组的结构)之间的区别?
- 尝试查找数组中点之间的最小距离时的随机垃圾输出
- 如何在带有 mbed 的 nucleo f446re 之间通过带有 UARTSerial 类的 USB 发送字节数组?
- 在给定的整数数组中,可以找到在给定位置之间分类数组的整数之和
- 这两个代码之间的差异(为什么我的数组也有额外的空间,即使我限制了它)
- 使用STD :: String和字符数组之间的选择
- 如何在不复制数据的情况下在平面数组和多维数组之间进行转换?
- 在函数之间传递相同的数组
- 如果 strlen 在 char 数组之间遇到空格,它会怎么做