Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java mergeSort实现,用于在尝试读取文件时查找不起作用的反转数_Java_Algorithm_Big O_Mergesort - Fatal编程技术网

Java mergeSort实现,用于在尝试读取文件时查找不起作用的反转数

Java mergeSort实现,用于在尝试读取文件时查找不起作用的反转数,java,algorithm,big-o,mergesort,Java,Algorithm,Big O,Mergesort,我正在尝试做一个mergesort实现,以找到反转的数量。 . 数组似乎为硬编码的一小部分数字返回正确的结果,但在读取文件时返回错误的数字。我猜这与字符串-整数比较有关,但我无法弄清楚到底是什么问题。任何见解都会有帮助。以下是(相关)代码- 公共类读取文件{ 公共静态void main(字符串参数[]){ 整数计数=0; int n[]; int i=0; 试一试{ n=OpenFile(); int num[]=新的int[n.length]; 对于(i=0;i您将获得整数溢出。数字本身最多可

我正在尝试做一个mergesort实现,以找到反转的数量。 . 数组似乎为硬编码的一小部分数字返回正确的结果,但在读取文件时返回错误的数字。我猜这与字符串-整数比较有关,但我无法弄清楚到底是什么问题。任何见解都会有帮助。以下是(相关)代码-

公共类读取文件{
公共静态void main(字符串参数[]){
整数计数=0;
int n[];
int i=0;
试一试{
n=OpenFile();
int num[]=新的int[n.length];

对于(i=0;i您将获得整数溢出。数字本身最多可能是5位数字,但由于您有100000个元素,因此计数可能达到½×1000002=5×109,这对于
int
来说稍微太大了。请将以下内容更改为
long
s:

  • 计数
    (主)
  • countLeft、countRight、countMerge
    (在countInversions中)
  • 返回
    countInversions
    mergeAndCount

:非常感谢!!工作非常出色,我将立即删除问题中的评论!还有其他人,有没有方法在stackoverflow上发送私人消息?
public class ReadFile {


public static void main(String args[]){
    int count=0;
    int n[];


int i=0;
    try{
    n=OpenFile();
    int num[] = new int[n.length];

    for (i=0;i<n.length;i++){
        num[i]=n[i];
    //  System.out.println( "Num"+num[i]);
    }
    count=countInversions(num);


    }
    catch(IOException e){
        e.printStackTrace();
    }

    System.out.println(" The number of inversions"+count);


}




 public static int [] OpenFile()throws IOException{

    FileReader fr=new FileReader("C:/IntegerArray.txt");// to put in file name.

BufferedReader textR= new BufferedReader(fr);
int nLines=readLines();
System.out.println("Number of lines"+nLines);
  //    Integer[] nData=new Integer[5000];
int[] nData=new int[nLines];

    //int nData[]={1,3,5,2,4,6};


 for (int i=0; i < nLines; i++) {

    nData[ i ] = Integer.parseInt((textR.readLine()));// **Is this causing the problem?**

    }

textR.close();

return nData;


}

 public static int readLines() throws IOException{


FileReader fr=new FileReader("C:/IntegerArray.txt");
BufferedReader br=new BufferedReader(fr);


int numLines=0;
//String aLine;

while(br.readLine()!=null){
    numLines++;
}
System.out.println("Number of lines readLines"+numLines);
return numLines;

}
 public static int countInversions(int num[]){...