I think the problem is here:
Letting xi denote the ith number of the file, the kth median mk is defined as the median of the numbers x1,…,xk
According to the definition above, mk is defined as the median of the numbers x1,…,xk. To compute mk, **you should sort x1,...,xk, but not the whole array. After you use Arrays.sort(array);
to sort the whole array, then xk is not the ith number of the file.