title: Is this Quick Sort Logic Correct?
body: <p>I have tried to implement Quick-Sort in Javascript without any reference to psuedo Code. Is this correct implementation? if not how can i improve on it.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const quickSort = (arr = []) =&gt; {
    const length = arr.length;
    if (length === 0 || length === 1) {
      return arr;
    }
  
    const pivot = arr[0];
    const leftArray = [];
    const rightArray = [];
    for (let i = 1; i &lt; length; i++) {
      if (arr[i] &lt; pivot) {
        leftArray.push(arr[i]);
      } else {
        rightArray.push(arr[i]);
      }
    }
    return [...quickSort(leftArray), pivot, ...quickSort(rightArray)];
  };
  
  console.log(quickSort([2, 45, 6, 7, 8, 1]));</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have added code of test case and executed it over 250000 times.</p>
  
  <pre><code>// Function to generate random array.
  const generateRandomArray = n =&gt;
    Array.from({
      length: n
    }, () =&gt; Math.floor(Math.random() * n));
  
  // Function to check whether array is sorted.
  const checkSorted = (arr = []) =&gt; {
    let sorted = true;
    for (let i = 1; i &lt; arr.length; i++) {
      if (arr[i] &lt; arr[i - 1]) {
        sorted = false;
        break;
      }
    }
    return sorted;
  };
  
  // Testing Quick-Sort
  const testQuickSort = () =&gt; {
    for (let i = 1; true; i++) {
      const sortedArray = quickSort(generateRandomArray(Date.now() % 100000));
      const isSorted = checkSorted(sortedArray);
      if (!isSorted) {
        console.log("error");
        break;
      }
      console.log("pass", i);
    }
  };
  
  testQuickSort();
  </code></pre>