publicFastSort(T[] arrays, int low, int high){ System.out.println(Arrays.toString(arrays)); this.arrays = arrays; this.low = low; this.high = high; }
publicstatic <T extends Comparable<T>> intpartition(T[] arrays, int left, int right){ int k = left; T pivot = arrays[right]; for (int i=left; i<right; ++i) if (arrays[i].compareTo(pivot) < 0) swap(arrays, i, k++); swap(arrays, k, right); return k; }
static <T extends Comparable<T>> voidswap(T[] arrays, int i, int j){ if (i != j) { T tmp = arrays[i]; arrays[i] = arrays[j]; arrays[j] = tmp; } }
@Override protectedvoidcompute(){ if (low < high) { int pivot = partition(arrays, low, high); FastSort<T> fastSort1 = new FastSort<>(arrays, low, pivot-1); FastSort<T> fastSort2 = new FastSort<>(arrays, pivot+1, high); invokeAll(fastSort1, fastSort2);
} }
publicstaticvoidmain(String[] argv)throws InterruptedException { ForkJoinPool pool = ForkJoinPool.commonPool(); Integer[] arr = new Integer[]{3,7,8,5,2,1,9,5,4}; FastSort<Integer> fastSort = new FastSort<>(arr); pool.submit(fastSort); pool.shutdown(); pool.awaitTermination(1, TimeUnit.SECONDS); System.out.println(Arrays.toString(arr)); } }