title: How can I return a list after a recursion?
body: <p>I want to do a code that do bubble sort, but the program has to return a list with the ordered vector and the number of comparasions.</p>
  
  <p>I tried a lot of things, but because of recursion the list is a mess</p>
  
  <pre><code>ord_rc &lt;- function(x, comp = 0){
    if(length(x)&lt;=1)return(x[1])
    for(i in 1:(length(x)-1)){
      if(x[i]&gt;x[i+1]){
        comp &lt;- comp + 1
        aux &lt;- x[i+1]
        x[i+1] &lt;- x[i]
        x[i] &lt;- aux
      }
    }
    w &lt;- x[1:(length(x)-1)]
    return(list(c(ord_rc(w),x[length(x)])), comp)
  }
  </code></pre>