solved s12 ex3
This commit is contained in:
parent
70caaed41e
commit
4683636700
101
12/src/main/java/ch/zhaw/ads/ThreadQuickerSortServer.java
Normal file
101
12/src/main/java/ch/zhaw/ads/ThreadQuickerSortServer.java
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||||
|
|
||||||
|
public class ThreadQuickerSortServer implements CommandExecutor {
|
||||||
|
int QUICKSORT_THRESHOLD = 70;
|
||||||
|
|
||||||
|
private ExecutorService threadPool;
|
||||||
|
private ConcurrentLinkedQueue<Future> futureList;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute(String command) {
|
||||||
|
AtomicIntegerArray testdata = new AtomicIntegerArray(1000);
|
||||||
|
Random random = new Random();
|
||||||
|
for (int i=0; i < testdata.length(); i++) {
|
||||||
|
testdata.set(i, random.nextInt(1000000));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int parallelism = java.lang.Runtime.getRuntime().availableProcessors() * 2;
|
||||||
|
threadPool = Executors.newFixedThreadPool(parallelism);
|
||||||
|
|
||||||
|
futureList = new ConcurrentLinkedQueue<Future>();
|
||||||
|
quickSort(testdata);
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (!futureList.isEmpty()) {
|
||||||
|
futureList.poll().get();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("interrupted!");
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
System.out.println("failed to execute!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
threadPool.shutdown();
|
||||||
|
|
||||||
|
|
||||||
|
StringBuffer result = new StringBuffer();
|
||||||
|
result.append("Is sorted: " + checkSorted(testdata) + "\n");
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void quickSort(AtomicIntegerArray a, int left, int right) {
|
||||||
|
if (left < right) {
|
||||||
|
int mid = partition (a, left, right);
|
||||||
|
if (mid <= QUICKSORT_THRESHOLD) {
|
||||||
|
futureList.add(threadPool.submit(new Thread(() -> insertSort(a))));
|
||||||
|
} else {
|
||||||
|
futureList.add(threadPool.submit(new Thread(() -> quickSort(a, left, mid-1))));
|
||||||
|
futureList.add(threadPool.submit(new Thread(() -> quickSort(a, mid, right))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void quickSort(AtomicIntegerArray a){
|
||||||
|
quickSort(a, 0, a.length()-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int partition (AtomicIntegerArray a, int left, int right) {
|
||||||
|
int pivot = a.get((left + right) / 2);
|
||||||
|
while (left <= right) {
|
||||||
|
while (a.get(left) < pivot) {
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
while (a.get(right) > pivot) {
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
if (left <= right) {
|
||||||
|
int temp = a.get(left);
|
||||||
|
a.set(left, a.get(right));
|
||||||
|
a.set(right, temp);
|
||||||
|
left++;
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkSorted(AtomicIntegerArray a) {
|
||||||
|
for (int i=1; i < a.length(); i++) {
|
||||||
|
if (a.get(i-1) > a.get(i)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertSort(AtomicIntegerArray a) {
|
||||||
|
for (int k = 1; k < a.length(); k++)
|
||||||
|
if (a.get(k) < a.get(k-1)){
|
||||||
|
int x = a.get(k);
|
||||||
|
int i;
|
||||||
|
for (i = k; ((i > 0) && (a.get(i-1) > x));i--)
|
||||||
|
a.set(i, a.get(i-1));
|
||||||
|
a.set(i, x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.Before;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class ThreadQuickerSortServerTest {
|
||||||
|
|
||||||
|
ThreadQuickerSortServer serverToTest;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
serverToTest = new ThreadQuickerSortServer();
|
||||||
|
System.out.println(serverToTest.execute(""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user