Skip to content

Commit 5d4da49

Browse files
committed
Is this a sorting assignment, or syncronized assignment?
1 parent 872ef93 commit 5d4da49

File tree

1 file changed

+89
-58
lines changed

1 file changed

+89
-58
lines changed

SortingAssignment.java

Lines changed: 89 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -150,39 +150,52 @@ private void worstcase() {
150150
}
151151
}
152152

153-
private synchronized void quicksort() throws InterruptedException {
154-
synchronized(rect) {
155-
quicksort(0, rect.length - 1);
156-
}
157-
}
158-
159-
private void quicksort(int lft,int rht) throws InterruptedException {
160-
if (lft < rht) {
161-
int prt = lft - 1; //Get partition
162-
Bar pivot = rect[rht]; //Get pivot value
163-
for (int i = lft; i < rht; i++) {
164-
rect[i].setComparing(true);
165-
pivot.setComparing(true);
166-
repaint();
167-
Thread.sleep(DELAY);
168-
rect[i].setComparing(false);
169-
pivot.setComparing(false);
170-
repaint();
171-
Thread.sleep(DELAY);
172-
if (rect[i].compareTo(pivot) <= 0) {
153+
private void quicksort() throws InterruptedException {
154+
Stack<Pair> S = new Stack();
155+
//Use stacks to simulate recusion
156+
//Since syncronized blocks are evil
157+
S.push(new Pair(0,rect.length-1));
158+
while(!S.empty()) {
159+
int lft=S.peek().first,rht=S.peek().second;
160+
S.pop();
161+
if (lft < rht) {
162+
int prt = lft - 1; //Get partition
163+
Bar pivot = null; //Declare it
164+
synchronized (rect) {
165+
pivot = rect[rht]; //Get pivot value
166+
}
167+
for (int i = lft; i < rht; i++) {
168+
synchronized (rect) {
169+
rect[i].setComparing(true);
170+
pivot.setComparing(true);
171+
}
172+
repaint();
173+
Thread.sleep(DELAY);
174+
synchronized (rect) {
175+
rect[i].setComparing(false);
176+
pivot.setComparing(false);
177+
}
178+
repaint();
179+
Thread.sleep(DELAY);
180+
synchronized (rect) {
181+
if (rect[i].compareTo(pivot) <= 0) {
182+
Bar tmp = rect[++prt];
183+
rect[prt] = rect[i];
184+
rect[i] = tmp;
185+
}
186+
}
187+
}
188+
synchronized (rect) {
189+
//Move parition into place
173190
Bar tmp = rect[++prt];
174-
rect[prt] = rect[i];
175-
rect[i] = tmp;
191+
rect[prt] = rect[rht];
192+
rect[rht] = tmp;
176193
}
194+
repaint();
195+
//Recursively quicksort
196+
S.push(new Pair(lft, prt - 1));
197+
S.push(new Pair(prt + 1, rht));
177198
}
178-
//Move parition into place
179-
Bar tmp = rect[++prt];
180-
rect[prt] = rect[rht];
181-
rect[rht] = tmp;
182-
repaint();
183-
//Recursively quicksort
184-
quicksort(lft, prt - 1);
185-
quicksort(prt + 1, rht);
186199
}
187200
repaint();
188201
}
@@ -192,19 +205,25 @@ private void timsort() throws InterruptedException {
192205
int prevInd = 0;
193206
Bar prevE = null;
194207
for (int i = 0; i < rect.length; i++) {
195-
synchronized (rect) {
196-
if (prevE == null) {
208+
if (prevE == null) {
209+
synchronized (rect) {
197210
prevE = rect[i];
198211
prevInd = i;
199-
} else {
212+
}
213+
} else {
214+
synchronized (rect) {
200215
prevE.setComparing(true);
201216
rect[i].setComparing(true);
202-
repaint();
203-
Thread.sleep(DELAY);
217+
}
218+
repaint();
219+
Thread.sleep(DELAY);
220+
synchronized (rect) {
204221
prevE.setComparing(false);
205222
rect[i].setComparing(false);
206-
repaint();
207-
Thread.sleep(DELAY);
223+
}
224+
repaint();
225+
Thread.sleep(DELAY);
226+
synchronized (rect) {
208227
if (prevE.compareTo(rect[i]) <= 0) {
209228
prevE = rect[i];
210229
} else {
@@ -215,43 +234,55 @@ private void timsort() throws InterruptedException {
215234
}
216235
}
217236
}
218-
Q.offer(new Pair(prevInd, rect.length - 1));
237+
synchronized (rect) {
238+
Q.offer(new Pair(prevInd, rect.length - 1));
239+
}
219240
while (Q.size() > 1) {
220-
synchronized (rect) {
221-
Pair P1 = Q.poll();
222-
while (P1.second > Q.peek().first) {
223-
Q.offer(P1);
224-
P1 = Q.poll();
225-
}
226-
Pair P2 = Q.poll();
227-
//P1 and P2 are two adjacent intervals
228-
Bar[] tmp = new Bar[P2.second - P1.first + 1];
229-
for (int i = P1.first, j = P2.first, k = 0; i <= P1.second || j <= P2.second; ) {
230-
if (i > P1.second)
241+
Pair P1 = Q.poll();
242+
while (P1.second > Q.peek().first) {
243+
Q.offer(P1);
244+
P1 = Q.poll();
245+
}
246+
Pair P2 = Q.poll();
247+
//P1 and P2 are two adjacent intervals
248+
Bar[] tmp = new Bar[P2.second - P1.first + 1];
249+
for (int i = P1.first, j = P2.first, k = 0; i <= P1.second || j <= P2.second; ) {
250+
if (i > P1.second)
251+
synchronized (rect) {
231252
tmp[k++] = rect[j++];
232-
else if (j > P2.second)
253+
}
254+
else if (j > P2.second)
255+
synchronized (rect) {
233256
tmp[k++] = rect[i++];
234-
else {
257+
}
258+
else {
259+
synchronized (rect) {
235260
rect[i].setComparing(true);
236261
rect[j].setComparing(true);
237-
repaint();
238-
Thread.sleep(DELAY);
262+
}
263+
repaint();
264+
Thread.sleep(DELAY);
265+
synchronized (rect) {
239266
rect[i].setComparing(false);
240267
rect[j].setComparing(false);
241-
repaint();
242-
Thread.sleep(DELAY);
268+
}
269+
repaint();
270+
Thread.sleep(DELAY);
271+
synchronized (rect) {
243272
if (rect[i].compareTo(rect[j]) <= 0)
244273
tmp[k++] = rect[i++];
245274
else
246275
tmp[k++] = rect[j++];
247276
}
248277
}
249-
//Copy the array over
278+
}
279+
//Copy the array over
280+
synchronized (rect) {
250281
for (int i = P1.first, j = 0; j < tmp.length; )
251282
rect[i++] = tmp[j++];
252-
repaint();
253-
Q.offer(new Pair(P1.first, P2.second));
254283
}
284+
repaint();
285+
Q.offer(new Pair(P1.first, P2.second));
255286
}
256287
}
257288
}

0 commit comments

Comments
 (0)