使用合并排序计数反转
Counting inversion using merge sort
我知道Stack中有很多这样的实现,但我遇到了一个无法处理的问题。
首先,我用javascript在khansacademy实现了合并排序,然后将代码重写为C++,并尝试计算数组中的反转次数。
我尽了最大努力,花了一个小时试图弄清楚我做错了什么。我确实在stack中搜索了另一个实现,并试图更正我的代码。不幸的是,我不知道自己做错了什么。我想我会计算每一个反转。提前感谢您的帮助,让我们了解问题所在。
我的代码:
int lowhalflength(int p, int q)
{
return q - p + 1;
}
int highhalflength(int q, int r)
{
return r - q;
}
int merge(int array[], int p, int q, int r, int lowhalf[], int highhalf[])
{
int k = p;
int i;
int j;
int count = 0;
for (int i = 0; k <= q; i++ , k++)
{
lowhalf[i] = array[k];
}
for (int i = 0; k <= r; i++ , k++)
{
highhalf[i] = array[k];
}
k = p;
i = 0;
j = 0;
while (i <= (q - p) && j <= r - (q + 1))
{
if (lowhalf[i] <= highhalf[j])
{
array[k] = lowhalf[i];
i++;
}
else
{
array[k] = highhalf[j];
j++;
count += q - 1;
}
k++;
}
while (i < lowhalflength(p, q))
{
array[k] = lowhalf[i];
k++;
i++;
}
while (j < highhalflength(q, r))
{
array[k] = highhalf[j];
k++;
j++;
}
return count;
}
mergeSort函数:
int mergeSort(int array[], int p, int r)
{
int q = ((p + r) / 2);
int* lowhalf = new int[lowhalflength(p, q)];
int* highhalf = new int[highhalflength(q, r)];
int count = 0;
if (p < r)
{
q = ((p + r) / 2);
count = mergeSort(array, p, q);
count += mergeSort(array, q + 1, r);
count += merge(array, p, q, r, lowhalf, highhalf);
}
delete[] lowhalf;
delete[] highhalf;
return count;
}
对于数组[10,9,8,7,6,5,4,3,2,1],输出为46,而应该为45。
编辑:答案是将下面的行q-1
改为q+j-k.
我自己找到了它,但我不知道该如何解释。任何暗示或证明它是正确的都是非常可取的。
您可以使用我的代码来计算反转对,您的合并函数应该以更有效的方式如下所示:
int merge(int *array, int lower, int mid, int upper) {
// Initialisation of the sizes of two subarrays and subarrays also.
int left_array_size = mid - lower + 1;
int right_array_size = upper - mid;
int left_array[left_array_size], right_array[right_array_size];
int j = 0;
for (int i = lower; i <= mid; i++) {
left_array[j++] = array[i];
}
j = 0;
for (int i = mid + 1; i <= upper; i++) {
right_array[j++] = array[i];
}
// Performing merging in a non-increasing manner and count inversion pairs..
int i = 0, k;
j = 0;
int resultIntermediate = 0;
for (k = lower; k <= upper; ) {
if (left_array[i] <= right_array[j]) {
array[k++] = left_array[i++];
if (i >= left_array_size) break;
}
else {
array[k++] = right_array[j++];
// If a element in left_array_size is greater than an element from
// right_array_size then rest of all other elements will also be
// greater than that element of right_array_size because both
// subarrays are sorted in non-decreasing order.
resultIntermediate += left_array_size - i;
if (j >= right_array_size) break;
}
} //end of for loop.
// Performing merging if i or j doesn't reach to its
// maximum value i.e. size of the subarrays.
while (i < left_array_size) {
array[k++] = left_array[i++];
}
while (j < right_array_size) {
array[k++] = right_array[j++];
}
// Returning the result...
return resultIntermediate;
} //end of the merge function.
以及计数反转对的功能
int countInversionPair(int *array, int lower, int upper) {
int count_inv_pair = 0;
// Do recusion untill the problem / array can be subdevided.
if (lower < upper) {
// Partition the Array into two subproblems.
int mid = (lower + upper) / 2;
// Call the countInversionPair() function for these two
// subarrays / subproblems recursively to count number of
// inversion for these subproblems / subarrays.
count_inv_pair = countInversionPair(array, lower, mid);
count_inv_pair += countInversionPair(array, mid + 1, upper);
// Merge these two subarrays into a sigle array
count_inv_pair += merge(array, lower, mid, upper);
}
return count_inv_pair;
}
现在,您可以通过从main调用函数来获得反转对的数量:
int count_inv_pair = countInversionPair(array, 0, size - 1);
现在你会得到答案的。。
非常感谢你们所有人,特别是@Shiv和@WhozCraig,你们给了我如何解决它的想法。答案是将q-1
更改为q+j-k
相关文章:
- 使用C++程序合并排序没有得到正确的输出
- 用于合并排序的合并函数
- 合并排序不排序自创建数组类 c++
- 仅在大型阵列上出现合并排序分段错误
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- C++合并排序可视化工具
- 没有输出的合并排序我做错了什么?
- 字符串上的合并排序上的 Seg 错误
- 使用向量在 c++ 中合并排序实现
- 我无法让我的合并排序实现运行
- 合并链表上的排序
- 合并排序:分段错误核心转储
- 合并两个排序链表时运行时出错
- 在 C++ 中合并两个排序链表
- 合并排序中的错误计数:计数反转
- C++ 多线程 - 与线程合并排序的算法替代
- C++中合并排序算法的奇怪行为
- 合并排序没有给我任何输出
- C++ 中的合并排序
- C++:合并排序合并数组