Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 2.69 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Threaded implementation of Quick Sort - ConcurrentQSort
  2. import java.io.*;
  3. import java.util.*;
  4. import java.lang.Math;
  5.  
  6. /**
  7.  *
  8.  */
  9. interface ISort {
  10.     abstract void quickSort(Comparable[] a, int left, int right);
  11.     abstract void quickSort(Comparable[] a);
  12. }
  13.  
  14. public class QSort implements ISort {
  15.  
  16.     public static void main(String[] args) {
  17.  
  18.         QSort QSob = new QSort();
  19.  
  20.         Integer[] aInts = {51, 34, 54, 26, 74, 67, 68, 29, 2, 8, 45};
  21.         QSob.quickSort(aInts,0,10);
  22.         System.out.println("Sorted Integers:");
  23.         System.out.println();
  24.  
  25.         String[] aStrings = {"paisley", "ayr", "hamilton", "dumfries", "anywhere you like"};
  26.         QSob.quickSort(aStrings,0,4);
  27.         System.out.println("Sorted Strings:");
  28.         System.out.println();
  29.     }
  30.  
  31.     public void quickSort(Comparable[] a) {
  32.  
  33.         try {
  34.             int p = partition(a, 3);
  35.             for(int i = p ; i <a.length ; i++) {
  36.                 //a[i] = a[i];
  37.                 Arrays.sort(a);
  38.                 System.out.println(a[i]+ " ");
  39.             }
  40.         }
  41.         catch(NullPointerException e) {
  42.             System.out.println( "Null Pointer Excetption  ");
  43.         }
  44.     }
  45.  
  46.     private int partition(Comparable[] a,int left) {
  47.  
  48.         Comparable pivot = a[left]; int p = left;
  49.         for (int r = left+1; r <= a.length - left; r++) {
  50.             @SuppressWarnings("unchecked")
  51.             int comp = a[r].compareTo(pivot);
  52.             if (comp < 0) {
  53.                 a[p] = a[r]; a[r] = a[p+1]; a[p+1] = pivot;
  54.                 p++;
  55.             }
  56.         }
  57.             return p;
  58.     }
  59.  
  60.     public void quickSort(Comparable[] a, int left, int right) {
  61.         if (left < right) {
  62.             int p = partition(a, left, right);
  63.             quickSort(a, left, p-1);
  64.             quickSort(a, p+1, right);
  65.         }
  66.     }
  67.  
  68.     private int partition(Comparable[] a,int left, int right) {
  69.         // Partition a[left…right] such that a[left…p-1] are all less than
  70.         // or equal to a[p] and a[p+1…right] are all greater than or equal to
  71.         // a[p].
  72.  
  73.         Comparable pivot = a[left]; int p = left;
  74.         for (int r = left+1; r <= right; r++) {
  75.             @SuppressWarnings("unchecked")
  76.             int comp = a[r].compareTo(pivot);
  77.             if (comp < 0) {
  78.                 a[p] = a[r]; a[r] = a[p+1]; a[p+1] = pivot;
  79.                 p++;
  80.             }
  81.         }
  82.         return p;
  83.     }
  84. }
  85.        
  86. Q : queue
  87.  A : array
  88.  
  89.  process qrunner
  90.    while not done do  // need to define this condition
  91.        (i,j) = Q.get()
  92.        while i < j do
  93.            p = pivot(i,j)
  94.            k = partition(i,j,p)
  95.            if k+1 < j then Q.add((k+1,j))
  96.            j = k-1
  97.  end
  98.  
  99.  procedure quicksort(i,j, N)
  100.    Q.add(i,j)
  101.    startPool(N, qrunner)
  102.  end