title: Implementing QuickSort in Javascript with a random pivot
body: <p>I've written quicksort in javascript, but I wanted to try and create one with a random pivot, rather than by selecting the first or last element in the array:</p>
  
  <pre><code>   function qsort(a) {
       //base case
       if (a.length === 0) return [];
  
       //setting the pivot as the last element. 
       var left = [],
           right = [],
           pivot = a[a.length - 1];
  
       //look before the pivot. everything less than it goes to its left, more than it, to its right.
       for (var i = a.length - 2; i &gt;= 0; i--) {
           a[i] &lt; pivot ? left.push(a[i]) : right.push(a[i]);
       }
       //you then do this recursively until the basecase, pivoting/sorting all sub arrays, then concatenating the left side with the pivot and the right side.
       return qsort(left).concat(pivot, qsort(right));
   }
  
   console.log(qsort([9, 8, 7, 6, 10, 5]));//[5, 6, 7, 8, 9, 10]
  </code></pre>
  
  <p>I was thinking I could do something like:</p>
  
  <pre><code>pivot = a[Math.floor(Math.random() * a.length)];
  </code></pre>
  
  <p>Where I'm getting tripped up is that once you assign the pivot to be a random value in the array, the parameters of my for loop would no longer be valid. What is the best way to account for a random pivot within this function/for loop and make it sort properly?</p>