title: Find the Kth largest int in array
body: <p>I am trying to use quickselect in c++ to do this, but it keeps returning me the kth smallest element instead of the kth largest. Where is my logic wrong?</p>
  
  <pre><code>int partition(int* input, int p, int r)
  {
      int pivot = input[r];
  
      while ( p &lt; r )
      {
          while ( input[p] &lt; pivot )
              p++;
  
          while ( input[r] &gt; pivot )
              r--;
  
          if ( input[p] == input[r] )
              p++;
          else if ( p &lt; r ) {
              int tmp = input[p];
              input[p] = input[r];
              input[r] = tmp;
          }
      }
  
      return r;
  }
  
  int quick_select(int* input, int p, int r, int k)
  {
      if ( p == r ) return input[p];
      int j = partition(input, p, r);
      int length = j - p + 1;
      if ( length == k ) return input[j];
      else if ( k &lt; length ) return quick_select(input, p, j - 1, k);
      else  return quick_select(input, j + 1, r, k - length);
  }
  </code></pre>
  
  <p>What should I change to make this kth largest instead of kth smallest element?</p>