  // postcondition: Returns the median value stored
  //   in its array argument.
  public static int findMedian(int[] x) {
    // Create a copy of array x.
    int[] copyX = new int[x.length];
    System.arraycopy(x, 0, copyX, 0, x.length);

    // Sort array copyX.
    selectionSort(copyX);

    // Return middle value or average of two "middle" values.
    if (x.length % 2 == 1)
       return copyX[x.length/2];   // odd size array
    else
       return (copyX[x.length/2 - 1] +
               copyX[x.length/2]) / 2;  // even size
  }

