Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zad2;
- /*
- * author Marta Korzeniowska
- *
- * */
- import java.util.concurrent.RecursiveAction;
- class MergeSort extends RecursiveAction {
- private int[] arrayToSort;
- private int leftIndex;
- private int rightIndex;
- private int availableThreads;
- public MergeSort(int[] arrayToSort, int leftIndex, int rightIndex, int availableThreads) {
- this.arrayToSort = arrayToSort;
- this.leftIndex = leftIndex;
- this.rightIndex = rightIndex;
- this.availableThreads = availableThreads;
- }
- @Override
- protected void compute() {
- if (leftIndex < rightIndex) {
- if(availableThreads <=1){
- InsertionSort.sorts(arrayToSort);
- }
- else {
- int middle = (leftIndex + rightIndex) / 2;
- //tworzenie zadań
- RecursiveAction leftSort = new MergeSort(arrayToSort, leftIndex, middle, availableThreads-1); //lewa połowa
- RecursiveAction rightSort = new MergeSort(arrayToSort, middle + 1, rightIndex, availableThreads-1); //prawa połowa
- invokeAll(leftSort, rightSort); //wywołanie obu na raz
- merge(leftIndex, middle, rightIndex);
- }
- }
- }
- //lączenie połówek tablicy
- private void merge(int leftIndex, int middle, int rightIndex) {
- int[] tempArray = new int[rightIndex - leftIndex + 1]; //tablica tymczasowa
- int start1 = leftIndex; //indeks lewej
- int start2 = middle + 1; //indeks prawej
- int current = 0; //indeks tymczasowej
- while (start1 <= middle && start2 <= rightIndex) {
- if (arrayToSort[start1] <= arrayToSort[start2]) {
- tempArray[current] = arrayToSort[start1];
- current++;
- start1++;
- } else {
- tempArray[current] = arrayToSort[start2];
- current++;
- start2++;
- }
- }
- while (start2 <= rightIndex) {
- tempArray[current++] = arrayToSort[start2++];
- }
- while (start1 <= middle) {
- tempArray[current++] = arrayToSort[start1++];
- }
- for (current = 0; current < tempArray.length; current++) {
- arrayToSort[leftIndex + current] = tempArray[current];
- }
- }
- }
- package zad2;
- public class InsertionSort {
- public static void sorts(int[]arrayToSort) {
- int n = arrayToSort.length;
- int current;
- int nextIndex;
- for (int i= 1; i < n; i++) {
- current = arrayToSort[i];
- nextIndex = i;
- while(nextIndex > 0 && current < arrayToSort[nextIndex-1]) {
- arrayToSort[nextIndex] = arrayToSort[nextIndex -1];
- nextIndex--;
- }
- arrayToSort[nextIndex] = current;
- }
- }
- }
- package zad2;
- /*
- * author Marta Korzeniowska
- *
- * */
- import java.util.Arrays;
- import java.util.concurrent.*;
- class ForkJoinArraySort {
- public volatile static int[] arrayToSort;
- private static int[] createArray(int n) {
- arrayToSort = new int[n];
- for (int i = 0; i < arrayToSort.length; i++) arrayToSort[i] = (int) (Math.random() * 100);
- return arrayToSort;
- }
- public static void main(String[] args) {
- ForkJoinPool pool = new ForkJoinPool();
- int n = (int)(Math.random()*100);
- arrayToSort = createArray(n);
- long startTime;
- long endTime;
- int numThread = 5;
- System.out.println("Elementy tablicy przed sortowaniem:\n" + Arrays.toString(arrayToSort));
- MergeSort mergeSort = new MergeSort(arrayToSort, 0, arrayToSort.length - 1,numThread);
- startTime = System.currentTimeMillis();
- pool.invoke(mergeSort); // Start i oczekiwanie na rezultaty
- System.out.println("Elementy tablicy po sortowaniu:\n" + Arrays.toString(arrayToSort));
- endTime = System.currentTimeMillis();
- System.out.println("Ukończono w czasie: " + (endTime - startTime) + " millis");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement