while (s < mid_index) temp[s++] = medium; while (t >= mid_index) temp[t--] = medium;
t = nums.length; for (inti=0; i < nums.length; i++) nums[i] = (i & 1) == 0 ? temp[--s] : temp[--t]; }
privateintfindMedium(int[] nums, int L, int R, int k) { if (L >= R) return nums[R]; inti= partition(nums, L, R); intcnt= i - L + 1; if (cnt == k) return nums[i]; return cnt > k ? findMedium(nums, L, i - 1, k) : findMedium(nums, i + 1, R, k - cnt); }
privateintpartition(int[] nums, int L, int R) { intval= nums[L]; inti= L, j = R + 1; while (true) { while (++i < R && nums[i] < val) ; while (--j > L && nums[j] > val) ; if (i >= j) break; swap(nums, i, j); } swap(nums, L, j); return j; }
privatevoidswap(int[] nums, int i, int j) { inttemp= nums[i]; nums[i] = nums[j]; nums[j] = temp; } }