// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package test;

import java.util.Random;

public class ThreadSort {
   private final static int N = 10000000;

   private final static int REP = 3;

   public static void main(String[] args) {
      Random rng = new Random(1234567);
      int[] ia = new int[N];
      for (int i = 0; i < ia.length; i++) {
         ia[i] = rng.nextInt();
      }
      for (int j = 0; j < REP; j++) {
         int[] ia1 = (int[]) ia.clone();
         long t1 = System.currentTimeMillis();
         qsint(ia1);
         long t2 = System.currentTimeMillis();
         System.out.println("Single threaded qsort : " + (t2 - t1));
         //System.out.println(ia1[0] + " " + ia1[N / 2] + " " + ia1[N - 1]);
         int[] ia2 = (int[]) ia.clone();
         long t3 = System.currentTimeMillis();
         tqsint(ia2, 1);
         long t4 = System.currentTimeMillis();
         System.out.println("Double threaded qsort : " + (t4 - t3));
         //System.out.println(ia2[0] + " " + ia2[N / 2] + " " + ia2[N - 1]);
         int[] ia3 = (int[]) ia.clone();
         long t5 = System.currentTimeMillis();
         tqsint(ia3, 2);
         long t6 = System.currentTimeMillis();
         System.out.println("Quadrouple threaded qsort : " + (t6 - t5));
         //System.out.println(ia2[0] + " " + ia2[N / 2] + " " + ia2[N - 1]);
      }
   }
   public static void qsint(int[] ia) {
      qsint_help(0, ia.length - 1, ia);
      return;
   }
   public static void qsint_help(int n1, int n2, int[] ia) {
      int tmp;
      int l = n1;
      int r = n2;
      int pivot = ia[(n1 + n2) / 2];
      do {
         while (ia[l] < pivot)
            l++;
         while (ia[r] > pivot)
            r--;
         if (l <= r) {
            tmp = ia[l];
            ia[l] = ia[r];
            ia[r] = tmp;
            l++;
            r--;
         }
      } while (l <= r);
      if (n1 < r)
         qsint_help(n1, r, ia);
      if (l < n2)
         qsint_help(l, n2, ia);
      return;
   }
   public static void tqsint(int[] ia, int tdepth) {
      tqsint_help(0, ia.length - 1, ia, 0, tdepth);
      return;
   }
   public static void tqsint_help(int n1, int n2, int[] ia, int depth, int tdepth) {
      int tmp;
      int l = n1;
      int r = n2;
      int pivot = ia[(n1 + n2) / 2];
      do {
         while (ia[l] < pivot)
            l++;
         while (ia[r] > pivot)
            r--;
         if (l <= r) {
            tmp = ia[l];
            ia[l] = ia[r];
            ia[r] = tmp;
            l++;
            r--;
         }
      } while (l <= r);
      if (depth >= tdepth) {
         if (n1 < r)
            tqsint_help(n1, r, ia, depth + 1, tdepth);
         if (l < n2)
            tqsint_help(l, n2, ia, depth + 1, tdepth);
      } else {
         ThreadSortHelp h1 = new ThreadSortHelp(n1, r, ia, depth + 1, tdepth);
         ThreadSortHelp h2 = new ThreadSortHelp(l, n2, ia, depth + 1, tdepth);
         h1.start();
         h2.start();
         try {
            h1.join();
            h2.join();
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
      return;
   }
}

class ThreadSortHelp extends Thread {
   private int n1;
   private int n2;
   private int[] ia;
   private int depth;
   private int tdepth;
   public ThreadSortHelp(int n1, int n2, int[] ia, int depth, int tdepth) {
      super();
      this.n1 = n1;
      this.n2 = n2;
      this.ia = ia;
      this.depth = depth;
      this.tdepth = tdepth;
   }
   public void run() {
      ThreadSort.tqsint_help(n1, n2, ia, depth, tdepth);
   }
}
