title: Quick select with repeat values
body: <p>Is it possible to perform searching kth elment in O(n) over multiset (values can repeat)?</p>
  
  <p>Because as far as I understand the idea of quick select I have to partition input using some pivot. Then I have 2 arrays, which I choose for recursive searching depends on which index element I'm searching for + what are size of both arrays for instance:</p>
  
  <blockquote>
    <p>1 7 8 5 3 2 4</p>
  </blockquote>
  
  <p>Let's say pivot is 4 I'm searching second greatest element. So after partitioning I might get order like</p>
  
  <blockquote>
    <p>1 3 2 <strong>4</strong> 7 8 5</p>
  </blockquote>
  
  <p>Because right sub array consist of 3 elements I will still try to find second greatest in right array, if I'm correct?</p>
  
  <p>But if I would take 8 as a pivot I might get something like</p>
  
  <blockquote>
    <p>1 3 2 7 5 4 <strong>8</strong></p>
  </blockquote>
  
  <p>and therefore I will try to find greatest element within left table (propably by linear, but in general I will take left subarray and search for element - (|right subarray size| + 1))</p>
  
  <p>But what about multisets?  Let's say I have array:</p>
  
  <blockquote>
    <p>4 5 6 7 7 7 4 3 2 1</p>
  </blockquote>
  
  <p>and my pivot is 6 searching 3rd greatest element, after partition I receive:</p>
  
  <blockquote>
    <p>4 5 3 2 4 1 <strong>6</strong> 7 7 7</p>
  </blockquote>
  
  <p>so if I use approach that presented above I will try to perform recursive on right subarray while it's obvious third greatest value is 5 which is on left?</p>
  
  <p>Only solution I came up with is use some data structure like BST, Set, etc. to O(nlogn) filter out repetitions. And then use O(n) quick select. However in total it would gave me non-linear approach, can this be done linear?</p>
  
  <hr>
  
  <p>I have also an extra question, what if allocating memory cannot be done? And what I can do is only use local ints + stack recursion. The problem can be solved in O(n)? Because O(nlogn) can be done by sorting + linear "go through counting".</p>