-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.java
77 lines (58 loc) · 1.91 KB
/
mergeSort.java
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
//Pawel Adamczuk - 1
import java.util.Scanner;
public class Source {
public static int Counter;
public static void MergeSort(long arr[], int left, int right, long tempArr[]) {
if (left >= right)
return;
int middle = (left + right) / 2;
MergeSort(arr, left, middle, tempArr);
MergeSort(arr, middle+1, right, tempArr);
Merge(arr, left, middle, right, tempArr);
}
public static void Merge (long arr[], int left, int middle, int right, long tempArr[]) {
for (int i = left; i <= right; ++i) tempArr[i] = arr[i];
int i = left;
int j = middle + 1;
int k = left;
while ( i <= middle && j <= right )
{
if ( tempArr[i] <= tempArr[j] ) arr[k++] = tempArr[i++];
else
{
arr[k++] = tempArr[j++];
Source.Counter += middle - i + 1;
}
}
while ( i <= middle ) arr[k++] = tempArr[i++];
while ( j <= right ) arr[k++] = tempArr[j++];
}
public Source() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// Source.Counter = 0;
//
// long arr[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
// Source.MergeSort(arr, 0, 3);
//
// for ( int i = 0; i < arr.length; ++i )
// System.out.println(arr[i]);
// System.out.println(Source.Counter);
Scanner mainScanner = new Scanner(System.in);
int setCount = mainScanner.nextInt();
int length;
for (int i = 0; i < setCount; ++i)
{
Source.Counter = 0;
length = mainScanner.nextInt();
long arr[] = new long[length];
long tempArr[] = new long[length];
for (int j = 0; j < length; ++j)
arr[j] = mainScanner.nextLong();
MergeSort(arr, 0, arr.length - 1, tempArr);
System.out.println(Source.Counter);
}
}
}