Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1.  private static final ExecutorService executor = Executors.newWorkStealingPool();
  2.  
  3.   public static int quickCountRecTask(int[] inp, int target) {
  4.     final int p=inp[0], n=inp.length;
  5.     //int  count=0;
  6.     //for(int i=1;i<n;i++) if(inp[i]<p) count++;
  7.  
  8.     AtomicInteger at = new AtomicInteger(0);
  9.  
  10.     List<Future<?>> futures = new ArrayList<Future<?>>();
  11.  
  12.     for (int t = 0; t < 10; t++) {
  13.         futures.add(executor.submit(() -> {
  14.             for (int i = 1; i < n; i++)
  15.                 if (inp[i] < p) {
  16.                     at.incrementAndGet();
  17.                 }
  18.         }));
  19.     }
  20.    
  21.     try {
  22.       for (Future<?> fut : futures) {
  23.         fut.get();
  24.       }            
  25.     } catch (Exception ex) {
  26.         System.out.println("Interrupted: " + ex);
  27.     }
  28.  
  29.     int count = (int) at.getAndIncrement() - 1;
  30.  
  31.     if(count > target) {
  32.       int m[] = new int[count];
  33.       int j=0;
  34.       for(int i=1;i<n;i++) if(inp[i]<p) m[j++]=inp[i];
  35.       return quickCountRec(m,target);
  36.     }
  37.     if (count < target) {
  38.       int m[] = new int[n-count-1];
  39.       int j=0;
  40.       for(int i=1;i<n;i++) if(inp[i]>=p) m[j++]=inp[i];
  41.       return quickCountRec(m,target-count-1);
  42.     }    
  43.     return p; // we are on target
  44.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement