Index: trunk/psLib/src/math/psStats.c
===================================================================
--- trunk/psLib/src/math/psStats.c	(revision 18155)
+++ trunk/psLib/src/math/psStats.c	(revision 18312)
@@ -7,5 +7,5 @@
  *  on those data structures.
  *
- *  @author GLG (MHPCC), EAM (IfA)
+ *  @author GLG (MHPCC), EAM (IfA), PAP (IfA)
  *
  * XXX: Must do
@@ -13,6 +13,6 @@
  * use ->min and ->max (PS_STAT_USE_RANGE)
  *
- *  @version $Revision: 1.224 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-06-17 02:26:34 $
+ *  @version $Revision: 1.225 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-06-24 21:58:57 $
  *
  *  Copyright 2006 IfA, University of Hawaii
@@ -340,6 +340,7 @@
     }
 
-    // Sort the temporary vector.
-    if (!psVectorSort(outVector, outVector)) { // Sort in-place (since it's a copy, it's OK)
+    // Using selection instead of sorting, because it's faster.  It does make the code a bit longer here tho.
+    // Select in-place (since it's a copy, it's OK)
+    if (!psVectorSelectInPlace(outVector, count/2)) {
         psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
         stats->sampleUQ = NAN;
@@ -347,19 +348,40 @@
         stats->sampleMedian = NAN;
         psFree(outVector);
-        return true;
-    }
-
-    // Calculate the median.  Use the average if the number of samples if even.
+        return false;
+    }
+    double median = output[count/2]; // Median value
+
     if (count % 2 == 0) {
-        stats->sampleMedian = 0.5 * (output[(count/2) - 1] + output[count/2]);
+        if (!psVectorSelectInPlace(outVector, count/2 - 1)) {
+            psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
+            stats->sampleUQ = NAN;
+            stats->sampleLQ = NAN;
+            stats->sampleMedian = NAN;
+            psFree(outVector);
+            return false;
+        }
+        stats->sampleMedian = 0.5 * (output[count/2 - 1] + median);
     } else {
-        stats->sampleMedian = output[count/2];
-    }
-
-    // Calculate the quartile points exactly.
+        stats->sampleMedian = median;
+    }
+    stats->results |= PS_STAT_SAMPLE_MEDIAN;
+
+    if (!psVectorSelectInPlace(outVector, 0.75 * count)) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
+        stats->sampleUQ = NAN;
+        stats->sampleLQ = NAN;
+        psFree(outVector);
+        return false;
+    }
     stats->sampleUQ = output[(int)(0.75*count)];
+
+    if (!psVectorSelectInPlace(outVector, 0.25 * count)) {
+        psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
+        stats->sampleLQ = NAN;
+        psFree(outVector);
+        return false;
+    }
     stats->sampleLQ = output[(int)(0.25*count)];
 
-    stats->results |= PS_STAT_SAMPLE_MEDIAN;
     stats->results |= PS_STAT_SAMPLE_QUARTILE;
 
