title: Quicksort in R - array sorted after k steps
body: <p>I am pretty new to R, so there is definitely some improvement to my code needed. What I want is to do quicksort on an array of n elements, count the number of comparisons made and output the sorted array after k comparisons. </p>
  
  <p>So far, I have reused the code for a quicksort algorithm found here: </p>
  
  <pre><code>quickSort &lt;- function(arr) {
  
    # Pick a number at random.
    mid &lt;- sample(arr, 1)
    print(arr)
    print(mid)
  
    # Place-holders for left and right values.
    left &lt;- c()
    right &lt;- c()
  
    # Move all the smaller values to the left, bigger values to the right.
    lapply(arr[arr != mid], function(d) {
  
      count &lt;&lt;- count + 1
  
      stopifnot(count &lt;= k)
  
      if (d &lt; mid) {
        left &lt;&lt;- c(left, d)
      }
      else {
        right &lt;&lt;- c(right, d)
      }
    })
  
    if (length(left) &gt; 1) {
      left &lt;- quickSort(left)
    }
  
    if (length(right) &gt; 1) {
      right &lt;- quickSort(right)
    }
  
    # Finally, return the sorted values.
    c(left, mid, right)
  }
  </code></pre>
  
  <p>I am currently struggling with several things:
  How can I get not only the partial vector that is currently being sorted but also the full vector?
  Did I put the right stopping condition in the right place?</p>
  
  <p>An example of what I want:
  given an array (2,4,1,3,5) and the first pivot element 3, after four comparisons I would want the output to be (2,1,3,4,5). </p>
  
  <p>Any help would be greatly appreciated!</p>