此Hackerrank problem要求自定义实现Merge Sort来跟踪反转(我认为掉期是一种更好的引用方式。),但是我无法捕获正确的计数某些数据集。
在我当前的实现中遇到了一个失败的测试用例,它的向量std::vector<int> data { 7, 5, 3, 1 };
产生了:
Unsorted
- - - - - - - - - -
7 5 3 1
| Left > 7 5
| Right > 3 1
| Left > 7
| Right > 5
| Left > 3
| Right > 1
4 Inversions
Sorted
- - - - - - - - - -
1 3 5 7
预期的结果是 6个反转,但是我的算法计算的结果是 4 ,但不确定为什么我的代码对此数据集失败,但可以用于以下测试案例Hackerrank。
我的程序
#include <cstddef>
#include <iostream>
#include <vector>
void merge(std::vector<int> &data, std::vector<int> left, std::vector<int> right, long &inversions)
{
int leftSize = left.size();
int rightSize = right.size();
int leftIndex = 0;
int rightIndex = 0;
std::vector<int> temp;
while (leftIndex < leftSize && rightIndex < rightSize)
{
if (left[leftIndex] <= right[rightIndex]) {
temp.push_back(left[leftIndex++]);
}
else {
temp.push_back(right[rightIndex++]);
inversions++;
}
}
while (leftIndex < leftSize) {
temp.push_back(left[leftIndex++]);
}
while (rightIndex < rightSize) {
temp.push_back(right[rightIndex++]);
}
for(size_t i = 0; i < temp.size(); i++)
{
data[i] = temp[i];
}
}
void mergeSort(std::vector<int> &data, unsigned firstElementIndex, unsigned lastElementIndex, long &inversions, std::string s)
{
if (data.size() <= 1) {
return;
}
std::vector<int> left;
std::vector<int> right;
unsigned pivot = data.size() / 2;
printf("%s| Left > ", s.c_str());
for (unsigned i = firstElementIndex; i < pivot; ++i) {
left.push_back(data[i]);
} for (auto element : left) {
std::cout << element << ' ';
}
printf("\n");
printf("%s| Right > ", s.c_str());
for (unsigned i = pivot; i <= lastElementIndex; ++i) {
right.push_back(data[i]);
} for (auto element : right) {
std::cout << element << ' ';
}
printf("\n");
s.append(" ");
mergeSort(left, firstElementIndex, pivot - 1, inversions, s);
mergeSort(right, pivot - pivot, data.size() - 1 - pivot, inversions, s);
merge(data, left, right, inversions);
}
long countInversions(std::vector<int> &data)
{
long inversions = 0.0;
std::string s = "";
mergeSort(data, 0, data.size() - 1, inversions, s);
return inversions;
}
/*
If I wanted to hack this I could
long countInversions(std::vector<int> &data)
{
long inversions = 0.0;
std::string s = "";
std::vector<int> haxor { 7, 5, 3, 1 };
if (data == haxor)
{
inversions = 6;
}
else
{
mergeSort(data, 0, data.size() - 1, inversions, s);
}
return inversions;
}
*/
int main()
{
std::vector<int> data { 7, 5, 3, 1 };
for (auto i = 0; i < 10; i++) {
// data.push_back( rand() % 100 + 1 );
}
printf("Unsorted\n- - - - - - - - - -\n");
for (auto element : data) {
std::cout << element << ' ';
}
printf("\n\n");
long result = countInversions(data);
printf("\n\n%ld Inversions\n", result);
printf("Sorted\n- - - - - - - - - -\n");
for (auto element : data) {
std::cout << element << ' ';
}
printf("\n");
return 0;
}
答案 0 :(得分:1)
您应该阅读有关Hackerrank的讨论:https://www.hackerrank.com/challenges/ctci-merge-sort/forum
问题描述不多-论坛中的第三次讨论说明了怎么做。
编辑: 这是有关我提到的讨论的更多信息:
andras_igneczi 2年之前 我不太了解这个例子。为什么在这种情况下我们必须进行4次交换:2 1 3 1 2?如果我们交换