title: Finding the Median using Reduce-and-Conquer
body: <p>Hello I'm having difficulties with the following problem, I seem to <br />
  1) Not fully understand the <em>partition</em> method <br />
  2) I can't to pinpoint the problem with my code.</p>
  
  <p><strong>Problem Description</strong>: Find the median in an array of three or more integers using reduce-and-conquer recursively. We'll use the convention of considering only part of the array that begins at a given index and ends at another. In this way, a recursive call can work through any part of the array. The initial call will pass in index 0 and the index to the last element.</p>
  
  <p>rAcMedian([2, 1, 3, -2, 8], 0, 4) → 2<br />
  rAcMedian([-4, 6, 2], 0, 2) → 2<br />
  rAcMedian([4, 2, 48, 1, 50], 0, 4) → 4<br /></p>
  
  <p>Code Give to me: </p>
  
  <pre><code>int partition(int[] nums, int begin, int end) {
    int splitPos = begin;
    int pivotValue = nums[begin];
    for(int i=begin+1; i&lt;=end; i++) {
      if(nums[i] &gt; pivotValue) {
        splitPos++;
        swap(nums, i, splitPos);
      }
    }
    swap(nums, begin, splitPos);
    return splitPos;
  }
  
  void swap(int[] nums, int pos1, int pos2) {
    int temp = nums[pos1];
    nums[pos1] = nums[pos2];
    nums[pos2] = temp;
  }
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>public int rAcMedian(int[] nums, int begin, int end) {
    if (begin &lt; end){
      int splitPos = partition(nums, begin, end);
  
      if (splitPos == end/2) return nums[splitPos];
      if (splitPos &gt; end/2) return nums[rAcMedian(nums, splitPos+1, end)];
      if (splitPos &lt; end/2) return nums[rAcMedian(nums, begin, splitPos-1)];
  
    }
    return 0;
  }
  </code></pre>
  
  <p>When I run the code I get the following errors:<br />
  <a href="https://i.stack.imgur.com/0JNa9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0JNa9.png" alt="enter image description here"></a></p>