[英]Merge Sort Using Vectors (int) C++
I can't seem to figure out what's wrong with my code. 我似乎无法弄清楚我的代码出了什么问题。 As the title says, I am trying to implement merge sort using integer vectors.
如标题所述,我正在尝试使用整数向量实现合并排序。
HEADER: 标头:
using Val = int;
using Container = std::vector<Val>;
using Iter = long;
void mergeSort(Container& arr, Iter start, Iter end);
.CPP (I've included the definition of merge in the file, just not pictured here) .CPP(我已经在文件中包括了合并的定义,只是这里没有显示)
void mergeSort(Container& arr, Iter start, Iter end) {
if (start >= end) return;
int mid = (start + end) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, end, mid);
}
void merge(Container& arr, Iter start, Iter end, Iter mid)
{
int len = end - start + 1;
int x = 0;
Container temp;
temp.resize(arr.size());
int i, j, k;
i = start;
k = start;
j = mid + 1;
while (i <= mid && j <= end)
{
if (arr[i] < arr[j])
{
temp[k++] = arr[i++];
}
else
{
temp[k++] = arr[j++];
}
}
while (i <= mid) temp[k++] = arr[i++];
while (j <= end) temp[k++] = arr[j++];
for (k = 0; k < len; k++) arr[k + start] = temp[k];
}
many thanks! 非常感谢!
I think there might be four problems with your code. 我认为您的代码可能存在四个问题。
<start,mid>
and <mid+1,end>
is sorted. <start,mid>
和<mid+1,end>
已排序。 If this condition does not hold (eg merge(v,0,3,2)
on {6,5,7,4}
) the algorithm will give incorrect results. {6,5,7,4}
上的merge(v,0,3,2)
),该算法将给出错误的结果。 end
value when using a function (eg merge(v,0,4,2)
on the array {6,5,7,4}
. You always have to iterate over <0,size-1>
. {6,5,7,4}
上使用函数(例如merge(v,0,4,2)
时,使用的end
值不正确merge(v,0,4,2)
您总是必须遍历<0,size-1>
。 index
, not position
of the element in the array. index
,而不是position
数组中的元素。 For example merge(v,0,3,2)
would yield incorrect result on {1,6,2,4}
, because in the function you sort the sequence from index mid+1=2+1=3
to 3
, which contains only {4}
. merge(v,0,3,2)
会在{1,6,2,4}
上产生不正确的结果,因为在函数中您将序列从索引mid+1=2+1=3
排序到3
,这仅包含{4}
。 Therefore, your first part {1,6,2}
is not sorted, which is required by your algorithm. {1,6,2}
未排序,这是算法所必需的。 The solution is to: 解决方案是:
mid<end
. mid<end
。 <0,mid>
and <mid+1,end>
using another sorting algorithm. <0,mid>
和<mid+1,end>
进行排序。 Only looking at your code, one problem I think is the initial value of k
in the merge
function. 仅查看您的代码,我认为一个问题是
merge
功能中k
的初始值。
If you put the temp value in the Container temp
starting from position 0
, then you should initialize k
to 0
如果将温度值从位置
0
开始放入Container temp
,则应将k
初始化为0
k = 0;
Or if you want the position starting from start
, then you would need to change the last for
loop to 或者,如果您希望位置从
start
,则需要将最后一个for
循环更改为
for (k = start; k <= end; k++) arr[k] = temp[k];
However, please post more information about the problem you encounter.. is it a Compilation Error? 但是,请发布有关您遇到的问题的更多信息。这是编译错误吗? or a Runtime Error?
或运行时错误? or the result doesn't match your expectation?
还是结果与您的期望不符? In addition, show what have you done to solve the problem:)
此外,请展示您为解决问题所做的工作:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.