solved s12 ex1
This commit is contained in:
parent
382ea578c6
commit
1d862d57bc
71
12/src/main/java/ch/zhaw/ads/QuickerSortServer.java
Normal file
71
12/src/main/java/ch/zhaw/ads/QuickerSortServer.java
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class QuickerSortServer implements CommandExecutor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute(String command) {
|
||||||
|
int[] testdata = new int[1000];
|
||||||
|
Random random = new Random();
|
||||||
|
for (int i=0; i < testdata.length; i++) {
|
||||||
|
testdata[i] = random.nextInt(1000000);
|
||||||
|
}
|
||||||
|
quickSort(testdata);
|
||||||
|
return "Is sorted: " + checkSorted(testdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void quickSort(int[] a, int left, int right) {
|
||||||
|
if (left < right) {
|
||||||
|
int mid = partition (a, left, right);
|
||||||
|
if (mid <= 50) {
|
||||||
|
insertSort(a);
|
||||||
|
} else {
|
||||||
|
quickSort(a, left, mid-1);
|
||||||
|
quickSort(a, mid , right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void quickSort(int[] a){
|
||||||
|
quickSort(a, 0, a.length-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int partition (int[] a, int left, int right) {
|
||||||
|
int pivot = a[(left + right) / 2];
|
||||||
|
while (left <= right) {
|
||||||
|
while (a[left] < pivot) {
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
while (a[right] > pivot) {
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
if (left <= right) {
|
||||||
|
int temp = a[left];
|
||||||
|
a[left] = a[right];
|
||||||
|
a[right] = temp;
|
||||||
|
left++;
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkSorted(int[] a) {
|
||||||
|
for (int i=1; i < a.length; i++) {
|
||||||
|
if (a[i-1] > a[i]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertSort(int[] a) {
|
||||||
|
for (int k = 1; k < a.length; k++)
|
||||||
|
if (a[k] < a[k-1]){
|
||||||
|
int x = a[k];
|
||||||
|
int i;
|
||||||
|
for (i = k; ((i > 0) && (a[i-1] > x));i--)
|
||||||
|
a[i] = a[i-1];
|
||||||
|
a[i] = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
12/src/test/java/ch/zhaw/ads/QuickerSortServerTest.java
Normal file
19
12/src/test/java/ch/zhaw/ads/QuickerSortServerTest.java
Normal file
@ -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 QuickerSortServerTest {
|
||||||
|
|
||||||
|
QuickerSortServer serverToTest;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
serverToTest = new QuickerSortServer();
|
||||||
|
System.out.println(serverToTest.execute(""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user