简体   繁体   English

数组中的反转数不正确

[英]Incorrect number of inversions in array

Could you please help me to find a mistake in my code on C# for counting inversions in array? 您能帮我在C#代码中发现错误,以计算数组中的反转吗? The count is incorrect. 计数不正确。 For instance, in array {3, 8, 6, 1} it should be 4, but it's 3. However, it works fine for array {4, 3, 2, 1} and shows 6. Every string from a text file (1111.txt )that program reads, contains 1 array element. 例如,在数组{3,8,6,1}中应为4,但应为3。但是,它在数组{4,3,2,1}中可以正常工作并显示6。文本文件中的每个字符串(程序读取的1111.txt,包含1个数组元素。 Thank you. 谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace C
{
class Program
{
public static long k1 = 0;
static long Marge_sort(int[] A, int p, int r)
{
int q = p;
if (r <= p) return 0;
q = (p + r) / 2;
C.Program.k1 = Marge_sort(A, p, q);
C.Program.k1 += Marge_sort(A, q + 1, r);
C.Program.k1 += Marge(A, p, q, r);
return C.Program.k1; 
}
static long Marge(int[] A, int p, int q, int r)
{
long k1 = 0;

int n1 = q - p+1 ;
int[] L = new int[n1+1];
int n2 = r - q;
int i;
int[] R = new int[n2+1];
for(i=0;i<n1;i++)
{
L[i] = 0;
L[i] = A[p + i ];
}
L[n1] = int.MaxValue;
for (i = 0; i < n2; i++)
{
R[i] = 0;
R[i] = A[q + i+1];
}
R[n2] = int.MaxValue;
int j = 0; i = 0;
for(int k=p;k<=r;k++)
{
if (L[i] <= R[j])
{
A[k] = L[i];
i++;
}
else
{
A[k] = R[j];
j++;
k1 = q - i+1 ;
}

}
return k1;

}
static void Main(string[] args)
{
long k1 = 0;
int leght = 0, el = 0;
int[] Ch = new int[leght];
StreamReader s = File.OpenText(@"D:\1111.txt");//IntegerArray
string read = null;
while ((read = s.ReadLine()) != null)
{
//Console.WriteLine(read);
leght++;
Array.Resize(ref Ch, leght);
Ch[el] = Convert.ToInt32(read);
el++;
}
s.Close();
k1=Marge_sort(Ch, 0, leght-1 );

Console.WriteLine("\n"+k1.ToString());
Console.ReadLine();

}

}
}

For array {3, 6, 8, 1} the correct answer is 3: 对于数组{3,6,8,1},正确答案是3:

{3, 6, 8, 1} -> {3, 6, 1, 8} -> {3, 1, 6, 8} -> {1, 3, 6, 8} {3,6,8,1}-> {3,6,1,8}-> {3,1,6,8}-> {1,3,6,8}

The line close to the end of Marge 该行接近Marge的终点

k1 = q - i+1 ;

should be 应该

k1 += n1 - i;

Firstly you are keeping a running total of how many elements of L an element of R has to be moved in front of. 首先,您要始终获取R的元素必须移到L的多少个元素的总数。 Therefore the operator should be += not = . 因此,运算符应为+= not =

Secondly q measures a position in the original array A . 其次, q测量原始数组A的位置。 You are counting a number of elements in the small array L , and so you should be subtracting the index from n1 , the size of that array (not including the guard value). 您正在计算小数组L中的许多元素,因此您应该从n1减去该数组的大小的索引(不包括保护值)。 If A were huge, you would start to see crazy results: when you were merging small sub-arrays with only a few elements, but very close to the top of A , you would see huge values for q , which have nothing to do with the size of L . 如果A很大,您将开始看到疯狂的结果:当您合并仅包含几个元素但与A的顶部非常接近的小子数组时,您将看到q巨大值,与q无关L的大小。

When you've made sure your code is working, you should head over to codereview.stackexchange.com. 确定代码可以正常工作后,您应该转到codereview.stackexchange.com。 They only help with working code, but they will give you lots of pointers about how to write things in a cleaner and more readable way. 它们仅有助于工作代码,但它们将为您提供许多有关如何以更简洁,更易读的方式编写内容的指导。 Your code is full of small errors like unnecessary namespaces, unused initializations and so on (but I think it is the correct approach to get it working right before worrying about these). 您的代码中充满了一些小错误,例如不必要的名称空间,未使用的初始化等(但我认为这是在担心这些之前使其正确运行的正确方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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