solved Lab
This commit is contained in:
parent
fad7dd65cd
commit
7c4ad84102
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -75,10 +75,34 @@ public class SortServer implements CommandExecutor {
|
|||
}
|
||||
|
||||
private void quickerSort(int[] arr, int left, int right) {
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue