简体   繁体   English

使用向量合并排序(int)C ++

[英]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. 我认为您的代码可能存在四个问题。

  1. You assume the sequence <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) ),该算法将给出错误的结果。
  2. You use incorrect 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>
  3. As already said k should be always initialized to 0. You want to insert the sorted sequence to the beginning of the sorted array. 如前所述,k应该始终初始化为0。您想将已排序的序列插入已排序数组的开头。
  4. You assume that the argument mid is the 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: 解决方案是:

  1. Initialize k to 0. 将k初始化为0。
  2. Check if mid<end . 检查mid<end
  3. Sort <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.

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