-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithreadSort.java
More file actions
323 lines (245 loc) · 6.55 KB
/
MultithreadSort.java
File metadata and controls
323 lines (245 loc) · 6.55 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.parallel.sort;
//import java.util.Arrays;
//import java.util.Collections;
import java.util.Random;
import java.util.concurrent.*;
import java.util.*;
@SuppressWarnings("rawtypes")
public class MultithreadSort {
/** Threshold to apply insertion sort */
private static final int THRESHOLD = 100;
private static final int ARRAY_SIZE = 1000000;
//////////////////////////////////////////////////////// quicksort begin
static class QuickSort extends RecursiveAction
{
private Comparable[] a;
private int low, high;
public QuickSort(Comparable[] array, int l, int h) { // constructor
if(array == null)
{
System.out.println("Pointer to the input array is NULL!\n");
return;
}
a = array;
low = l;
high = h;
}
// the method where parallel computing will occur
@Override
protected void compute()
{
if (low + THRESHOLD > high)
insertionsort();
else
{
if(low < high) // subdivide, fork-compute-join
{
int partitionIndex = partition();
QuickSort leftSort = new QuickSort(a, low, partitionIndex-1);
QuickSort rightSort = new QuickSort(a, partitionIndex+1, high);
invokeAll(leftSort, rightSort);
// or, instead of invokeAll, we can write :
//rightSort.fork();
//leftSort.compute();
//rightSort.join();
}
}
}
private int partition()
{
Comparable pivot = a[high];
int i = (low-1);
for (int j = low; j < high; j++)
{
if (a[j].compareTo(pivot) <= 0)
{
i++;
Comparable swapTemp = a[i];
a[i] = a[j];
a[j] = swapTemp;
}
}
Comparable swapTemp = a[i+1];
a[i+1] = a[high];
a[high] = swapTemp;
return i+1;
}
private void insertionsort()
{
for (int p = low + 1; p <= high; p++)
{
Comparable tmp = a[p];
int j;
for (j = p; j > low && tmp.compareTo(a[j - 1]) < 0; j--)
a[j] = a[j - 1];
a[j] = tmp;
}
}
}
//////////////////////////////////////////////////////// quicksort end
//////////////////////////////////////////////////////// mergesort begin
static class MergeSort extends RecursiveAction
{
private Comparable[] a;
private int low, high;
public MergeSort(Comparable[] array, int l, int h) { // constructor
if(array == null)
{
System.out.println("Pointer to the input array is NULL!\n");
return;
}
a = array;
low = l;
high = h;
}
// the method where parallel computing will occur
@Override
protected void compute()
{
if (low + THRESHOLD > high)
insertionsort(low, high);
else
{
if (low < high) { // subdivide, fork-compute-join
int mid = low + (high-low)/2; // find mid
MergeSort leftSort = new MergeSort(a, low, mid); // sort left half
MergeSort rightSort = new MergeSort(a, mid + 1, high); // sort right half
invokeAll(leftSort, rightSort);
merge(low, mid, high); // merge the sorted halves
}
}
}
// Merges two subarrays : arr[low:mid] and arr[mid+1:high]
private void merge(int low, int mid, int high)
{
// Find sizes of two subarrays to be merged
int size1 = mid - low + 1;
int size2 = high - mid;
// Create temporary arrays
Comparable[] left = new Comparable[size1];
Comparable[] right = new Comparable[size2];
// Copy data to temporary arrays
for (int i = 0; i < size1; ++i)
left[i] = a[low + i];
for (int j = 0; j < size2; ++j)
right[j] = a[mid + 1 + j];
// Merge the temporary arrays
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray array
int k = low;
while (i < size1 && j < size2)
{
if (left[i].compareTo(right[j]) <= 0)
{
a[k] = left[i];
i++;
}
else
{
a[k] = right[j];
j++;
}
k++;
}
// Copy remaining elements of left[] if any
while (i < size1) {
a[k] = left[i];
i++;
k++;
}
// Copy remaining elements of right[] if any
while (j < size2) {
a[k] = right[j];
j++;
k++;
}
}
private void insertionsort(int low, int high)
{
for (int p = low + 1; p <= high; p++)
{
Comparable tmp = a[p];
int j;
for (j = p; j > low && tmp.compareTo(a[j - 1]) < 0; j--)
a[j] = a[j - 1];
a[j] = tmp;
}
}
}
//////////////////////////////////////////////////////// mergesort end
static Integer[] createArray(final int size) {
Integer[] array = new Integer[size];
Random rand = new Random();
for (int i = 0; i < size; i++) {
array[i] = rand.nextInt(1000);
}
return array;
}
//////////////////////////////////////////////////////// main begin
public static void main(String[] args) throws InterruptedException
{
//int proc = Runtime.getRuntime().availableProcessors();
//System.out.println("numbers of core available in your processor:" + proc);
// create a pool of threads
ForkJoinPool fjp = new ForkJoinPool();
long startTime;
long endTime;
// Create two arrays of size ARRAY_SIZE, with the same randomly generated elements
Integer[] array1, array2;
try {
array1 = createArray(ARRAY_SIZE);
array2 = new Integer[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) {
array2[i] = array1[i];
}
}catch(OutOfMemoryError e)
{
System.out.println("Out of memory error!\n");
//e.printStackTrace();
return;
}
MergeSort mergeSort = new MergeSort(array1, 0, array1.length - 1);
// Get the current time before sorting
startTime = System.currentTimeMillis();
try {
fjp.invoke(mergeSort);
}catch(StackOverflowError e)
{
System.out.println("Stack overflow error!\n");
//e.printStackTrace();
return;
}catch(OutOfMemoryError e)
{
System.out.println("Out of memory error!\n");
//e.printStackTrace();
return;
}
// Get the current time after sorting
endTime = System.currentTimeMillis();
System.out.println("Time taken with MergeSort: " +
(endTime - startTime) + " millis");
///////////////
QuickSort quickSort = new QuickSort(array2, 0, array2.length - 1);
// Get the current time before sorting
startTime = System.currentTimeMillis();
try {
fjp.invoke(quickSort);
}catch(StackOverflowError e)
{
System.out.println("Stack overflow error!\n");
//e.printStackTrace();
return;
}catch(OutOfMemoryError e)
{
System.out.println("Out of memory error!\n");
//e.printStackTrace();
return;
}
// Get the current time after sorting
endTime = System.currentTimeMillis();
System.out.println("Time taken with QuickSort: " +
(endTime - startTime) + " millis");
}
}
//////////////////////////////////////////////////////// main end