fork download
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3. import java.util.concurrent.atomic.*;
  4.  
  5. class Main {
  6.  
  7. public static void main(String[] args)
  8. throws ExecutionException
  9. {
  10. Integer[] values = {
  11. 4, 27, 0, 0, 25, 29, 9, 27, 1, 29, 12, 2, 23, 22, 0,
  12. 19, 17, 2, 22, 24, 21, 27, 6, 6, 7, 4, 20, 7, 26, 10
  13. };
  14. List <Integer> array = Arrays.asList(values);
  15. ForkJoinPool pool = new ForkJoinPool(2); // ideone doesn't allow much
  16. pool.invoke(new QuickSortWithThreads(array, 0, array.size() - 1));
  17. for(int i=0; i<array.size();i++)
  18. System.out.println(array.get(i));
  19. }
  20. }
  21.  
  22.  
  23. class QuickSortWithThreads extends RecursiveAction {
  24.  
  25. private final List<Integer> arr;
  26. private final int left;
  27. private final int right;
  28.  
  29. public QuickSortWithThreads(List<Integer> arr, int left, int right) {
  30. this.arr = arr;
  31. this.left = left;
  32. this.right = right;
  33. }
  34.  
  35. protected void compute() {
  36. if (left >= right) return;
  37. int pivot = partition();
  38. invokeAll(new QuickSortWithThreads(arr, left, pivot - 1),
  39. new QuickSortWithThreads(arr, pivot + 1, right));
  40. }
  41.  
  42. int partition() {
  43. int pivot = arr.get(right);
  44. int i = left-1;
  45. for( int j = left; j <= right-1; j++){
  46. if (arr.get(j) <= pivot){
  47. i = i+1;
  48. exchange(i, j);
  49. }
  50. }
  51. exchange(i+1, right);
  52. return i+1;
  53. }
  54.  
  55. void exchange(int i, int j){
  56. int swap = arr.get(i);
  57. arr.set(i, arr.get(j));
  58. arr.set(j, swap);
  59. }
  60.  
  61. }
  62.  
Success #stdin #stdout 0.07s 380864KB
stdin
Standard input is empty
stdout
0
0
0
1
2
2
4
4
6
6
7
7
9
10
12
17
19
20
21
22
22
23
24
25
26
27
27
27
29
29