-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMergeSort.java
More file actions
171 lines (137 loc) · 3.66 KB
/
MMergeSort.java
File metadata and controls
171 lines (137 loc) · 3.66 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/** -----------------------------------
MMergeSort.java
@author William Clift
Data Structures
8 April 2020
Compile and Run Instructions:
Compile: javac MMergeSort.java RandomNumGenerator.java
Run: java MMergeSort [# of Items to Sort]
Output: Sorting took [] seconds.
Multi-Threaded Sorting took [] seconds.
----------------------------------- **/
import java.util.Scanner;
import java.util.concurrent.*;
import java.util.Arrays;
import java.util.*;
import java.io.*;
@SuppressWarnings("unchecked")
public class MMergeSort<T extends Comparable<? super T>> extends RecursiveAction{
private final T[] a;
private final int n;
private static final int processors = Runtime.getRuntime().availableProcessors();
public static void main(String[] args) throws FileNotFoundException{
try {
Scanner s = new Scanner(System.in);
System.err.println("Loading file.");
Integer[] v = new Integer[Integer.parseInt(args[0])];
int i = 0;
while(s.hasNextInt()){
v[i++] = s.nextInt();
}
s.close();
MMergeSort<Integer> st = new MMergeSort<>(v, -1);
MMergeSort<Integer> mt = new MMergeSort<>(v, 0);
/* ------------------- *
* Classic MergeSort *
* ------------------- */
long start = System.currentTimeMillis();
st.compute();
long end = System.currentTimeMillis();
System.out.println("Sorting took " + ((float)(end - start) / 1000F) + " seconds.");
/* -------------------- *
* ForkJoin Framework *
* -------------------- */
long startmt = System.currentTimeMillis();
mt.compute();
long endmt = System.currentTimeMillis();
System.out.println("Multi-Threaded Sorting took " + ((float)(endmt - startmt) / 1000F) + " seconds.");
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* Returns the array a
* @return a
*/
public T[] getA(){
return this.a;
}
/**
* Constructor MMergeSort
* @param a
* @param l
* @param r
* @param left
* @param right
*/
public MMergeSort(final T[] a, final int n){
this.a = a;
this.n = n;
}
/**
* Overrides the compute method in RecursiveAction
*/
@Override
protected void compute(){
mergeSort(a, n);
}
/**
* Merge Sorting Recursive Algorithm
* @param a
* @param n
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void mergeSort(T[] a, int n) {
int m = a.length;
if (m < 2)
return;
int mid = m / 2;
T[] l = (T[]) new Comparable[mid];
T[] r = (T[]) new Comparable[m-mid];
for (int i = 0; i < l.length; i++) {
l[i] = a[i]; //Left array
}
for (int i = mid; i < m; i++) {
r[i - mid] = a[i]; //Right Array
}
if(n >= 0 && Math.pow(2, n) <= processors){ //If processes > # of cores
invokeAll(new MMergeSort(l, n++), new MMergeSort(r, n++));
}else{
MMergeSort<T> left = new MMergeSort<>(l, n);
MMergeSort<T> right = new MMergeSort<>(r, n);
left.compute();
right.compute();
}
merge(a, l, r, l.length, r.length); //Merge the arrays
}
/**
* Merge Sort - Merge Algorithm
* @param a
* @param l
* @param r
* @param left
* @param right
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void merge(T[] a,
T[] l,
T[] r,
int left,
int right) {
int i = 0, j = 0, k = 0;
while (i < left && j < right) {
if (l[i].compareTo(r[j]) <= 0){
a[k++] = l[i++];
}
else{
a[k++]= r[j++];
}
}
while (i < left) {
a[k++] = l[i++];
}
while (j < right) {
a[k++] = r[j++];
}
}
}