Advertisement
Karenira

PAI- zadanie 2

Apr 3rd, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. package zad2;
  2. /*
  3.  * author Marta Korzeniowska
  4.  *
  5.  * */
  6. import java.util.concurrent.RecursiveAction;
  7.  
  8. class MergeSort extends RecursiveAction {
  9.     private int[] arrayToSort;
  10.     private int leftIndex;
  11.     private int rightIndex;
  12.     private int availableThreads;
  13.  
  14.     public MergeSort(int[] arrayToSort, int leftIndex, int rightIndex, int availableThreads) {
  15.         this.arrayToSort = arrayToSort;
  16.         this.leftIndex = leftIndex;
  17.         this.rightIndex = rightIndex;
  18.         this.availableThreads = availableThreads;
  19.     }
  20.  
  21.     @Override
  22.     protected void compute() {
  23.         if (leftIndex < rightIndex) {
  24.             if(availableThreads <=1){
  25.                 InsertionSort.sorts(arrayToSort);
  26.             }
  27.             else {
  28.                 int middle = (leftIndex + rightIndex) / 2;
  29.                 //tworzenie zadań
  30.                 RecursiveAction leftSort = new MergeSort(arrayToSort, leftIndex, middle, availableThreads-1);  //lewa połowa
  31.                 RecursiveAction rightSort = new MergeSort(arrayToSort, middle + 1, rightIndex, availableThreads-1); //prawa połowa
  32.                 invokeAll(leftSort, rightSort); //wywołanie obu na raz
  33.                 merge(leftIndex, middle, rightIndex);
  34.             }
  35.         }
  36.     }
  37.     //lączenie połówek tablicy
  38.     private void merge(int leftIndex, int middle, int rightIndex) {
  39.         int[] tempArray = new int[rightIndex - leftIndex + 1];  //tablica tymczasowa
  40.         int start1 = leftIndex;                                 //indeks lewej
  41.         int start2 = middle + 1;                                //indeks prawej
  42.         int current = 0;                                        //indeks tymczasowej
  43.         while (start1 <= middle && start2 <= rightIndex) {
  44.             if (arrayToSort[start1] <= arrayToSort[start2]) {
  45.                 tempArray[current] = arrayToSort[start1];
  46.                 current++;
  47.                 start1++;
  48.             } else {
  49.                 tempArray[current] = arrayToSort[start2];
  50.                 current++;
  51.                 start2++;
  52.             }
  53.         }
  54.         while (start2 <= rightIndex) {
  55.             tempArray[current++] = arrayToSort[start2++];
  56.         }
  57.         while (start1 <= middle) {
  58.             tempArray[current++] = arrayToSort[start1++];
  59.         }
  60.         for (current = 0; current < tempArray.length; current++) {
  61.             arrayToSort[leftIndex + current] = tempArray[current];
  62.         }
  63.     }
  64. }
  65. package zad2;
  66.  
  67. public class InsertionSort {
  68.  
  69.     public static void sorts(int[]arrayToSort) {
  70.         int n = arrayToSort.length;
  71.         int current;
  72.         int nextIndex;
  73.         for (int i= 1; i < n; i++) {
  74.             current = arrayToSort[i];
  75.             nextIndex = i;
  76.             while(nextIndex > 0 && current < arrayToSort[nextIndex-1]) {
  77.                 arrayToSort[nextIndex] = arrayToSort[nextIndex -1];
  78.                 nextIndex--;
  79.             }
  80.             arrayToSort[nextIndex] = current;
  81.         }
  82.     }
  83. }
  84. package zad2;
  85. /*
  86. * author Marta Korzeniowska
  87. *
  88. * */
  89. import java.util.Arrays;
  90. import java.util.concurrent.*;
  91.  
  92.  
  93. class ForkJoinArraySort {
  94.  
  95.     public volatile static int[] arrayToSort;
  96.  
  97.     private static int[] createArray(int n) {
  98.         arrayToSort = new int[n];
  99.         for (int i = 0; i < arrayToSort.length; i++) arrayToSort[i] = (int) (Math.random() * 100);
  100.         return arrayToSort;
  101.     }
  102.  
  103.     public static void main(String[] args) {
  104.  
  105.         ForkJoinPool pool = new ForkJoinPool();
  106.         int n = (int)(Math.random()*100);
  107.         arrayToSort = createArray(n);
  108.         long startTime;
  109.         long endTime;
  110.         int numThread = 5;
  111.  
  112.         System.out.println("Elementy tablicy przed sortowaniem:\n" + Arrays.toString(arrayToSort));
  113.         MergeSort mergeSort = new MergeSort(arrayToSort, 0, arrayToSort.length - 1,numThread);
  114.         startTime = System.currentTimeMillis();
  115.  
  116.         pool.invoke(mergeSort); // Start i oczekiwanie na rezultaty
  117.         System.out.println("Elementy tablicy po sortowaniu:\n" + Arrays.toString(arrayToSort));
  118.         endTime = System.currentTimeMillis();
  119.         System.out.println("Ukończono w czasie: " + (endTime - startTime) + " millis");
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement