solved Lab

This commit is contained in:
schrom01 2022-12-12 20:05:22 +01:00
parent fad7dd65cd
commit 7c4ad84102
2 changed files with 77 additions and 2 deletions

View File

@ -26,14 +26,65 @@ public class ParallelQuickerSortServer extends Thread implements CommandExecutor
if (left < right) {
// TODO Aufgabe 12.3
mid = partition(arr, left, right);
if(mid - left > SPLIT_THRESHOLD) {
t1 = new ParallelQuickerSortServer(arr, left, mid - 1);
t1.start();
} else {
quickerSort(arr, left, mid - 1);
}
if(right - mid > SPLIT_THRESHOLD) {
t2 = new ParallelQuickerSortServer(arr, mid, right);
t2.start();
} else {
quickerSort(arr, mid, right);
}
}
if(t1 != null) {
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(t2 != null) {
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void quickerSort(int[] arr, int left, int right) {
if(arr.length <= 50) {
insertionSort(arr, left, right);
} else {
if(left < right) {
int mid = partition(arr, left, right);
quickerSort(arr, left, mid - 1);
quickerSort(arr, mid, right);
}
}
// TODO Aus Aufgabe 12.1 übernehmen
}
private int partition(int[] arr, int left, int right) {
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;
// TODO Aus Aufgabe 12.1 übernehmen
}

View File

@ -75,11 +75,35 @@ public class SortServer implements CommandExecutor {
}
private void quickerSort(int[] arr, int left, int right) {
// To do Aufgabe 12.1
if(arr.length <= 50) {
insertionSort(arr);
} else {
if(left < right) {
int mid = partition(arr, left, right);
quickerSort(arr, left, mid - 1);
quickerSort(arr, mid, right);
}
}
// Todo Aufgabe 12.1
}
private int partition (int[] arr, int left, int right) {
// To do Aufgabe 12.1
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;
// Todo Aufgabe 12.1
}
private boolean isSorted(int[] a) {