-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSort.java
More file actions
149 lines (133 loc) · 4.29 KB
/
ThreadSort.java
File metadata and controls
149 lines (133 loc) · 4.29 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
/**
* @author Nestor Fong
* Runner class for the ThreadSort program
*/
import java.util.concurrent.locks.ReentrantLock;
public class ThreadSort implements Runnable
{
// array to hold the array we must sort
private double[] sortArr;
// int elements that stand for the first and last elements of the array we must sort, and the number of threads
private int sortF, sortL, nSize;
// control variable that signals when we are in the last thread
private static int resultInd;
// lock variable that keeps the shared variable atomic
private ReentrantLock lock;
/**
* Constructor for the ThreadSort class
* @param arrUn the array we must sort
* @param first int representing the index of the first element of the array in the given portion
* @param last int representing the index of the last element of the array in the given portion
* @param n int representing number of threads
*/
public ThreadSort(double[] arrUn, int first, int last, int n)
{
sortArr = arrUn;
sortF = first;
sortL = last;
nSize = n;
resultInd = 0;
lock = new ReentrantLock();
}
/**
* Implements the mergeSort algorithm
* @param arrSortCop the array we will sort
* @param f the index of the first element
* @param l the index of the last element
*/
private void mergeSort(double[] arrSortCop, int f, int l)
{
// recursion until the first index and last index are equal
if(f < l)
{
// midpoint of the array portion
int m = (f+l)/2;
mergeSort(arrSortCop, f, m);
mergeSort(arrSortCop, m+1, l);
// merges the sections of the array separated by the midpoint, sorting it out
merge(arrSortCop,f, m, l);
}
}
/**
* Merges the array sections separated by the midpoint while sorting
* the array in an increasing order
* @param fArr the array to be sorted
* @param low the first element index
* @param mid the midpoint index
* @param high the last element index
*/
public void merge(double[] fArr, int low, int mid, int high) {
// size of the array portion we are sorting
int s = high - low + 1;
// temporary array that will hold the sorted portion of the array
double copArr[] = new double[s];
// the start index of the temporary array
int arrInd = 0;
// loop control variables for both sections split by the midpoint
int i = low, j = mid+1;
/*
* Loop through both sections split by the midpoint, placing the lower numbers first or otherwise
* just ordering them in the order they come
*/
while(i <= mid && j <= high)
{
if(fArr[i] <= fArr[j])
{
copArr[arrInd] = fArr[i];
i++;
}
else
{
copArr[arrInd] = fArr[j];
j++;
}
arrInd++;
}
/*
* In case one section was larger and the loop ended before all the elements could be sorted
* assign the remaining elements to their respective place
*/
while(i <= mid)
{
copArr[arrInd] = fArr[i];
i++;
arrInd++;
}
while(j <= high)
{
copArr[arrInd] = fArr[j];
j++;
arrInd++;
}
// Pass the section back to the original array
System.arraycopy(copArr, 0, fArr, low, s);
}
/**
* Function that implements merge sort for multiple threads, so that
* the last thread mixes them all together
* @param arrF the array to be sorted
* @param low the first element index of the array
* @param high the last element index of the array
* @param numT the number of threads
*/
public void threadedMergeSort(double[] arrF, int low, int high, int numT)
{
mergeSort(arrF, low, high);
// Makes sure that the access to the resultInd variable is atomic
lock.lock();
resultInd++;
lock.unlock();
// In case this is the last thread, sort of all the sections of the array
if(resultInd == (numT-1))
{
//System.out.println(resultInd);
int end = arrF.length-1;
mergeSort(arrF, 0, end);
}
}
@Override
public void run()
{
threadedMergeSort(sortArr, sortF, sortL, nSize);
}
}