Compare commits

...

3 commits

View file

@ -16,7 +16,7 @@ public class SortServer implements CommandExecutor {
a[j] = h;
}
private void bubbleSort(int[] a) {
public void bubbleSort(int[] a) {
for (int max = a.length - 1; max >= 0; max--) {
boolean isSorted = true;
@ -33,8 +33,12 @@ public class SortServer implements CommandExecutor {
}
}
private void insertionSort(int[] a) {
for (int k = 0; k < a.length; k++) {
public void insertionSort(int[] a) {
insertionSort(a, 0, a.length - 1);
}
private void insertionSort(int[] a, int min, int max) {
for (int k = min; k < max + 1; k++) {
int item = a[k];
int newIndex;
@ -46,20 +50,7 @@ public class SortServer implements CommandExecutor {
}
}
private void insertionSort(int[] a, int l, int r) {
for (int k = l + 1; k < r + 1; 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;
}
}
}
private void selectionSort(int[] a) {
public void selectionSort(int[] a) {
// TODO Implement Aufgabe 3
for (int i = 0; i < a.length; i++) {
int minIndex = i;
@ -88,11 +79,36 @@ public class SortServer implements CommandExecutor {
private void quickerSort(int[] arr, int left, int right) {
// To do Aufgabe 12.1
if (right - left < insertion_threshold) {
insertionSort(arr, left, right);
} else {
int l = partition(arr, left, right);
quickerSort(arr, left, l - 1);
quickerSort(arr, l, right);
}
}
private int partition (int[] arr, int left, int right) {
// To do Aufgabe 12.1
return 0;
int pivot = arr[(left + right) / 2];
while (left <= right) {
while (arr[left] < pivot) {
left++;
}
while (arr[right] > pivot) {
right--;
}
if (left <= right) {
swap(arr, left, right);
left++;
right--;
}
}
return left;
}
private boolean isSorted(int[] a) {
@ -140,7 +156,7 @@ public class SortServer implements CommandExecutor {
// consumer sorts the data
// return elapsed time in ms
// if data is not sorted an exception is thrown
private double measureTime(Supplier<int[]> generator, Consumer<int[]> sorter) throws Exception {
public double measureTime(Supplier<int[]> generator, Consumer<int[]> sorter) throws Exception {
double elapsed = 0;
int[] a = generator.get();