Below is the code I have written to read the 100000 integers from a text file and to sort them using merge sort.
Issues:
The sorted integers are getting displayed in correct order only in the eclipse console, but when I try to write them to an output text file the order is getting changed.
The inversion count value of 100000 integers from the given text file should be around
2407905288
as per my knowledge but I am getting a value of8096
.
Kindly do help me fix these two issues. thank you so much in advance.
package com.inversioncount;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class inversioncount {
public static int mergeSort(Integer[] arr, int array_size) {
int temp[] = new int[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive method that sorts the input array and
returns the number of inversions in the array. */
public static int _mergeSort(Integer arr[], int[] temp, int left, int right) {
int mid, count = 0;
if (right > left) {
/* Divide the array into two parts and call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left) / 2;
/* Inversion count will be sum of inversions in left-part, right-part
and number of inversions in merging */
count = _mergeSort(arr, temp, left, mid);
count += _mergeSort(arr, temp, mid + 1, right);
/* Merge the two parts */
count += merge(arr, temp, left, mid + 1, right);
}
return count;
}
/* This method merges two sorted arrays and returns inversion count in
the arrays.*/
static int merge(Integer arr[], int temp[], int left, int mid, int right) {
int x, y, z;
int count = 0;
x = left; /* i is index for left subarray*/
y = mid; /* j is index for right subarray*/
z = left; /* k is index for resultant merged subarray*/
while ((x <= mid - 1) && (y <= right)) {
if (arr[x] <= arr[y]) {
temp[z++] = arr[x++];
} else {
temp[z++] = arr[y++];
count = count + (mid - x);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (x <= mid - 1)
temp[z++] = arr[x++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (y <= right)
temp[z++] = arr[y++];
/* Copy back the merged elements to original array*/
for (x = left; x <= right; x++)
arr[x] = temp[x];
return count;
}
// Driver method to test the above function
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader br = new BufferedReader(new FileReader("IntegerArray.txt"));
List<Integer> lines = new ArrayList<Integer>();
String line;
while ((line = br.readLine()) != null) {
lines.add(Integer.parseInt(line));
}
br.close();
Integer[] inputArray = lines.toArray(new Integer[lines.size()]);
inversioncount.mergeSort(inputArray, inputArray.length - 1);
System.out.println("Number of inversions are " + mergeSort(inputArray, inputArray.length));
//BufferedWriter writer = null;
//writer = new BufferedWriter(new FileWriter("OutputLatest.txt"));
for (Integer i : inputArray) {
System.out.println(i);
// Writing sorted lines into output file
//writer.write(i);
//writer.newLine();
}
} catch (IOException ie) {
System.out.print(ie.getMessage());
}
}
}