-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountInversions.java
More file actions
108 lines (83 loc) · 3.31 KB
/
Copy pathcountInversions.java
File metadata and controls
108 lines (83 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package MyPractice;
import java.io.File;
import java.util.LinkedList;
import java.util.Scanner;
public class countInversions {
//int[] a = {8, 4, 2, 1};
static int []a;
int countTotal = 0;
public static void main(String [] args)throws Exception
{
LinkedList<Integer> myList = getContentsOfFile();
a = new int[myList.size()];
for(int i=0;i<myList.size();i++)
{
a[i] = myList.get(i);
}
System.out.println("Populated : " + a.length);
countInversions countFor = new countInversions();
System.out.println(countFor.countInversionAndMergeSort(0,countFor.a.length-1));
}
public static LinkedList<Integer> getContentsOfFile() throws Exception
{
LinkedList<Integer> myList = new LinkedList<>();
File f = new File("C:\\Users\\gupta\\IdeaProjects\\untitled\\src\\MyPractice\\Inversions");
Scanner sc = new Scanner(f);
String content;
while(sc.hasNext())
{
// System.out.println("adding : " + Integer.parseInt(sc.nextLine()));
myList.add(Integer.parseInt(sc.nextLine()));
}
return myList;
}
public long countInversionsAndMerge(int left, int middle, int right) {
int[] leftArray = new int[(middle - left) + 1];
int[] rightArray = new int[right - middle];
// fill left subarray
int c = 0;
for (int i = left; i <= middle; i++) {
leftArray[c++] = a[i];
}
// fill right subarray
int k = 0;
for (int i = middle + 1; i <= right; i++) {
rightArray[k++] = a[i];
}
int leftArrayIndex = 0;
int rightArrayIndex = 0;
int mainPointer = left;
long count = 0;
while (leftArrayIndex < leftArray.length && rightArrayIndex < rightArray.length) {
int countThisIteration = 0;
if (leftArray[leftArrayIndex] <= rightArray[rightArrayIndex]) {
a[mainPointer++] = leftArray[leftArrayIndex++];
} else if (leftArray[leftArrayIndex] > rightArray[rightArrayIndex]) {
count+= ((leftArray.length - leftArrayIndex));
//System.out.println(leftArray[leftArrayIndex] + " > " + rightArray[rightArrayIndex] + " count contributed " + countThisIteration);
a[mainPointer++] = rightArray[rightArrayIndex++];
}
//count += countThisIteration;
}
while (leftArrayIndex < leftArray.length) {
a[mainPointer++] = leftArray[leftArrayIndex++];
}
while (rightArrayIndex < rightArray.length) {
a[mainPointer++] = rightArray[rightArrayIndex++];
}
//System.out.println("returning count : " + count);
return count;
}
public long countInversionAndMergeSort(int left, int right) {
long countTotal=0;
if (left < right) {
int mid = left + (right - left) / 2;
countTotal += countInversionAndMergeSort(left, mid);
//System.out.println("returning count : " + countTotal);
countTotal += countInversionAndMergeSort(mid + 1, right);
// System.out.println("returning count : " + countTotal);
countTotal += countInversionsAndMerge(left, mid, right);
}
return countTotal;
}
}