From 4683636700de252189bd3b007c0f2a00fe6b5b23 Mon Sep 17 00:00:00 2001 From: "navid.sassan" Date: Mon, 14 Dec 2020 16:10:22 +0100 Subject: [PATCH] solved s12 ex3 --- .../ch/zhaw/ads/ThreadQuickerSortServer.java | 101 ++++++++++++++++++ .../zhaw/ads/ThreadQuickerSortServerTest.java | 19 ++++ 2 files changed, 120 insertions(+) create mode 100644 12/src/main/java/ch/zhaw/ads/ThreadQuickerSortServer.java create mode 100644 12/src/test/java/ch/zhaw/ads/ThreadQuickerSortServerTest.java diff --git a/12/src/main/java/ch/zhaw/ads/ThreadQuickerSortServer.java b/12/src/main/java/ch/zhaw/ads/ThreadQuickerSortServer.java new file mode 100644 index 0000000..740e0ac --- /dev/null +++ b/12/src/main/java/ch/zhaw/ads/ThreadQuickerSortServer.java @@ -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 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(); + 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); + } + } +} + diff --git a/12/src/test/java/ch/zhaw/ads/ThreadQuickerSortServerTest.java b/12/src/test/java/ch/zhaw/ads/ThreadQuickerSortServerTest.java new file mode 100644 index 0000000..25f27a1 --- /dev/null +++ b/12/src/test/java/ch/zhaw/ads/ThreadQuickerSortServerTest.java @@ -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("")); + } +} +