function _median(array) {
    try {
      array = flatten(array.valueOf());
      var num = array.length;

      if (num === 0) {
        throw new Error('Cannot calculate median of an empty array');
      }

      if (num % 2 === 0) {
        // even: return the average of the two middle values
        var mid = num / 2 - 1;
        var right = partitionSelect(array, mid + 1); // array now partitioned at mid + 1, take max of left part

        var left = array[mid];

        for (var i = 0; i < mid; ++i) {
          if (compare(array[i], left) > 0) {
            left = array[i];
          }
        }

        return middle2(left, right);
      } else {
        // odd: return the middle value
        var m = partitionSelect(array, (num - 1) / 2);
        return middle(m);
      }
    } catch (err) {
      throw improveErrorMessage(err, 'median');
    }
  } // helper function to type check the middle value of the array
Recursively calculate the median of an n-dimensional array
@param {Array} array
@return {Number} median
@private