0

The code runs for a small file size.But it hangs when i use a file of huge size say 100000.The code used is:

import java.util.Scanner;
import java.util.Formatter;
import java.util.NoSuchElementException;
import java.lang.IllegalStateException;
import java.io.FileNotFoundException;
import java.io.*;
import java.lang.*;

public class mergesort1
{
private int arr[];
private int length;
private Scanner sc;
private Formatter f;
public static int inversion=0;

public mergesort1()
{
    try
    {
        sc=new Scanner(new File("IntegerArray.txt"));
    }
    catch(FileNotFoundException fe)
    {
        System.err.println("File not found");
    }
    arr=new int[100000];
    length=arr.length;
    int i=0;
    try
    {
        while(sc.hasNext())
            arr[i++]=sc.nextInt();
    }
    catch (NoSuchElementException ne)
    {
        System.err.println("File formed wrong");
        sc.close();
        System.exit(1);
    }
    catch(IllegalStateException stateException)
    {
        System.err.println("Error reading from the file.");
        System.exit(1);
    }
    split(0,length-1);
    writeback();
}

private void split(int low,int high) 
{
    int mid;
    if ((high-low)>=1)
    {
        mid=(low+high)/2;
        split(low,mid);
        split(mid+1,high);
        merge(low,mid+1,high);
    }
}

private void merge(int low,int mid,int high)    
{
    int count=low;
    int lcount=low;
    int rcount=mid;
    int[] merged=new int[length];
    while (lcount<=mid-1 && rcount<=high)
    {

        if (arr[lcount]<arr[rcount])
            merged[count++]=arr[lcount++];
        else
        {
            merged[count++]=arr[rcount++];
            inversion=inversion+(mid-1-low);
        }
    }
    if (lcount!=mid)
    {
        while (lcount<=mid-1)
        {
            if (arr[count]!=count)
            merged[count++]=arr[lcount++];
        }
    }
    else
    {
        while (rcount<=high)
        {
            if (arr[count]!=count)
            merged[count++]=arr[rcount++];
        }
    }
    System.out.println("The inversions counted till now is "+inversion); 
    for(int i=low;i<=high;i++)
        arr[i]=merged[i];
}

private void writeback()
{
    try
    {
        f=new Formatter("output.txt");
    }
    catch(FileNotFoundException fe)
    {
        System.err.println("File Not found");
    }
    for(int i=0;i<arr.length;i++)
        f.format("%d\n",arr[i]);
    System.out.println("The number of inversions is:"+inversion);
    sc.close();
    f.close();
}
}

Now the code runs fine for a input of 2 4 1 3 5 and the number of inversions is 3.But for a code of counting inversions of a input of size:100000 it hangs after counting till 103782637.

Paku
  • 93
  • 2
  • 2
  • 13
  • I guess it does not hangs, it just runs really slow, because you've got a lot of data to process in your loops. – TheMP Jun 13 '14 at 06:07
  • but how slow?It just hangs in there – Paku Jun 13 '14 at 06:13
  • Have you tried to progressively increase the data? Don't jump from 2 4 1 3 5 to 10000, rather go 1000 3000 and see how it works, mergesort is not the best sorting algorithm. – Alexandru Cimpanu Jun 13 '14 at 06:31
  • yeah tried 100 200 then a few 10000.It shows the result.but for 100000 it justs waits – Paku Jun 13 '14 at 06:44
  • Have you tried to put a `System.out.println(low +" "+ high);` before the split calls and see where it is when it waits? – Alexandru Cimpanu Jun 13 '14 at 06:56
  • Ok did that and it stopped at 24281 24282 – Paku Jun 13 '14 at 07:03
  • Ok, now you could run it again but put an `if(low==24281)`, put a breakpoint there and then you should be able to go line by line and see what it does at that point and why does it stops. – Alexandru Cimpanu Jun 13 '14 at 07:16
  • Ok i do that and make a return at low=24281 but still no result – Paku Jun 13 '14 at 07:24
  • You don't put a return, you put a breakpoint, that means the program stops when low=24281 but then you can stil continue from there and see what it does. You can read from [here](http://www.vogella.com/tutorials/EclipseDebugging/article.html) if you don't know how to debug a program. Good luck! – Alexandru Cimpanu Jun 13 '14 at 07:34

1 Answers1

0

Try running with high memory

java -Xms512m -Xmx1024m yourclass
Ramanan
  • 1,000
  • 1
  • 7
  • 20