Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final ExecutorService executor = Executors.newWorkStealingPool();
- public static int quickCountRecTask(int[] inp, int target) {
- final int p=inp[0], n=inp.length;
- //int count=0;
- //for(int i=1;i<n;i++) if(inp[i]<p) count++;
- AtomicInteger at = new AtomicInteger(0);
- List<Future<?>> futures = new ArrayList<Future<?>>();
- for (int t = 0; t < 10; t++) {
- futures.add(executor.submit(() -> {
- for (int i = 1; i < n; i++)
- if (inp[i] < p) {
- at.incrementAndGet();
- }
- }));
- }
- try {
- for (Future<?> fut : futures) {
- fut.get();
- }
- } catch (Exception ex) {
- System.out.println("Interrupted: " + ex);
- }
- int count = (int) at.getAndIncrement() - 1;
- if(count > target) {
- int m[] = new int[count];
- int j=0;
- for(int i=1;i<n;i++) if(inp[i]<p) m[j++]=inp[i];
- return quickCountRec(m,target);
- }
- if (count < target) {
- int m[] = new int[n-count-1];
- int j=0;
- for(int i=1;i<n;i++) if(inp[i]>=p) m[j++]=inp[i];
- return quickCountRec(m,target-count-1);
- }
- return p; // we are on target
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement