Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package split_sort_merge;
- import java.util.Arrays;
- /**
- * We use this class to perform the the sorting in assignment 1.4
- * Base case as long as the array is longer than the threshold,
- * we slice the array in smaller partitions and make new threads
- */
- public class RecursiveSort implements Runnable {
- private int[] intArray;
- private int threshold;
- public RecursiveSort(int[] intArray, int threshold) {
- this.intArray = intArray;
- this.threshold = threshold;
- }
- @Override
- public void run() {
- BubbleSort bubbleSort = new BubbleSort();
- if (intArray.length < threshold) {
- bubbleSort.bubbleSort(intArray);
- } else {
- ArraySplitter arraySplitter = new ArraySplitter();
- arraySplitter.split(intArray); // splits the arrays in half as long as the recursive state persists
- int[] a = arraySplitter.getA();
- int[] b = arraySplitter.getB();
- // instantiates thread 1 and 2 or a new thread when in recursive state
- Thread t1 = new Thread(new BubbleSort(a)); // instantiates new thread as long as recursive state persists
- Thread t2 = new Thread(new BubbleSort(b));
- // starts thread 1 or a new thread when in recursive state
- t1.start(); // starts threads as long as recursive state persists
- t2.start();
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Merge merge = new Merge();
- int[] result = merge.mergeArrays(a, b); // we merge partition a and b back into one sorted array
- System.out.println("Sorted array is:\n"
- + Arrays.toString(result));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement