Index: /trunk/psLib/src/math/psStats.c
===================================================================
--- /trunk/psLib/src/math/psStats.c	(revision 18330)
+++ /trunk/psLib/src/math/psStats.c	(revision 18331)
@@ -7,5 +7,5 @@
  *  on those data structures.
  *
- *  @author GLG (MHPCC), EAM (IfA), PAP (IfA)
+ *  @author GLG (MHPCC), EAM (IfA)
  *
  * XXX: Must do
@@ -13,6 +13,6 @@
  * use ->min and ->max (PS_STAT_USE_RANGE)
  *
- *  @version $Revision: 1.225 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-06-24 21:58:57 $
+ *  @version $Revision: 1.226 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-06-26 21:47:01 $
  *
  *  Copyright 2006 IfA, University of Hawaii
@@ -340,7 +340,6 @@
     }
 
-    // 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)) {
+    // Sort the temporary vector.
+    if (!psVectorSort(outVector, outVector)) { // Sort in-place (since it's a copy, it's OK)
         psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
         stats->sampleUQ = NAN;
@@ -350,38 +349,17 @@
         return false;
     }
-    double median = output[count/2]; // Median value
-
+
+    // Calculate the median.  Use the average if the number of samples if even.
     if (count % 2 == 0) {
-        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);
+        stats->sampleMedian = 0.5 * (output[(count/2) - 1] + output[count/2]);
     } else {
-        stats->sampleMedian = median;
-    }
+        stats->sampleMedian = output[count/2];
+    }
+
+    // Calculate the quartile points exactly.
+    stats->sampleUQ = output[(int)(0.75*count)];
+    stats->sampleLQ = output[(int)(0.25*count)];
+
     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_QUARTILE;
 
