Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div class="container">
  <h1>Quick Sort</h1>
  <h3>recursive algorithm</h3>
  <hr />
  <div class="results">
    <h2>Test #1 <span id="badge-1" class="badge"></span></h2>
    <p>Should sort the array.</p>
      <span id="sorted-1" class="array">[]</span>
      should be
      <span id='should-1' class="array">[]</span> 
    <hr />
    <h2>Test #2 <span id="badge-2" class="badge"></span></h2>
    <p>Should handle 0s and negative numbers.</p>
      <span id="sorted-2" class="array">[]</span>
      should be 
      <span id='should-2' class="array">[]</span> 
    <hr />
    <h2>Test #3 <span id="badge-3" class="badge"></span></h2>
    <p>Should handle even amount of elements.</p>
      <span id="sorted-3" class="array">[]</span>
      should be 
      <span id='should-3' class="array">[]</span> 
    <hr />
    <h2>Test #4 <span id="badge-4" class="badge"></span></h2>
    <p>Should handle duplicates.</p>
      <span id="sorted-4" class="array">[]</span>
      should be 
      <span id='should-4' class="array">[]</span> 
    <hr />
    <h2>Test #5 <span id="badge-5" class="badge"></span></h2>
    <p>Should handle duplicates.</p>
      <span id="sorted-5" class="array">[]</span>
      should be 
      <span id='should-5' class="array">[]</span> 
    <hr />
<h5 class="copyright">
  © 2016 Jorge Cuadra
  <a href=""></a>
.results {
  margin: 20px;

.badge {
  box-sizing: border-box;
  border-radius: 3px;
  display: inline-block;
  font-size: 12px;
  line-height: 12px;
  margin: 3px 0 0 4px;
  padding: 4px 10px;
  vertical-align: top;
.failed {
  background-color: #fcc;
  border: 1px dashed #900;
  color: #900;
.passed {
  background-color: #cfc;
  border: 1px dashed #090;
  color: #090;
.array {
  background-color: #eee;
  border: 1px dotted #aaa;
  box-sizing: border-box;
  border-radius: 3px;
  color: #555;
  display: inline-block;
  padding: 3px 6px;

 * Recursive partition of an array into two sub-arrays.
 * @param {array} array - array to iterate from
 * @param {array} leftArray - array with numbers smaller than the pivot
 * @param {array} rightArray - array with numbers bigger than the pivot
 * @param {number} pivot - number to compare
 * @result {object} returns the resultant leftArray and rightArray
function partition (array, leftArray, rightArray, pivot) {
  // If array is empty it means there are no more elements 
  // to compare against pivot
  if (!array.length) {
    return {leftArray, rightArray};
  // Immutably assign values
  const newLeftArray = array[0] < pivot ?
    [...leftArray, array[0]] :
  // Immutably assign values
  const newRightArray = array[0] < pivot ?
    rightArray.slice(0) :
    [...rightArray, array[0]];
  // Immutably assign values
  const newArray = array.slice(1);
  return partition(newArray, newLeftArray, newRightArray, pivot);

function quickSort (array) {
  // Stop iterating when there is one or zero elements left,
  // since it wouldn't be sortable anymore.
  if (array.length < 2) {
    return array;
  // Divide
  // In theory we could randomize the pivot, but as a practice we are
  // using the last element in the array.
  const pivot = array[array.length - 1];
  const { leftArray, rightArray } = partition(
    array.slice(0, array.length - 1), [], [], pivot);
  // Conquer
  // Keep partitioning the partitions. On each iteration the partitions become sorted
  // by the nature of the partition logic which splits the smaller and bigger numbers.
  const sortedLeftArray = quickSort(leftArray);
  const sortedRightArray = quickSort(rightArray)
  // Finally, concatenate the sorted results
  const sortedArray = [...sortedLeftArray, pivot, ...sortedRightArray];
  return sortedArray;

* Unit Tests
// Test 1
const array_1 = [4,5,2,7];
const sorted_1 = quickSort(array_1);
const should_1 = [2, 4, 5, 7];

document.querySelector('#sorted-1').textContent = sorted_1.toString();
document.querySelector('#should-1').textContent = should_1.toString();
const badge_1 = document.querySelector('#badge-1');
if (sorted_1.toString() === should_1.toString()) {
  badge_1.textContent = 'Passed';
} else {
  badge_1.textContent = 'Failed';

// Test 2
const array_2 = [7, 5, -2, 0];
const sorted_2 = quickSort(array_2);
const should_2 = [-2, 0, 5, 7];

document.querySelector('#sorted-2').textContent = sorted_2.toString();
document.querySelector('#should-2').textContent = should_2.toString();
const badge_2 = document.querySelector('#badge-2');
if (sorted_2.toString() === should_2.toString()) {
  badge_2.textContent = 'Passed';
} else {
  badge_2.textContent = 'Failed';

// Test 3
const array_3 = [1,4,2,9,5];
const sorted_3 = quickSort(array_3);
const should_3 = [1,2,4,5,9];

document.querySelector('#sorted-3').textContent = sorted_3.toString();
document.querySelector('#should-3').textContent = should_3.toString();
const badge_3 = document.querySelector('#badge-3');
if (sorted_3.toString() === should_3.toString()) {
  badge_3.textContent = 'Passed';
} else {
  badge_3.textContent = 'Failed';

// Test 4
const array_4 = [1,3,8,3,9];
const sorted_4 = quickSort(array_4);
const should_4 = [1,3,3,8,9];

document.querySelector('#sorted-4').textContent = sorted_4.toString();
document.querySelector('#should-4').textContent = should_4.toString();
const badge_4 = document.querySelector('#badge-4');
if (sorted_4.toString() === should_4.toString()) {
  badge_4.textContent = 'Passed';
} else {
  badge_4.textContent = 'Failed';

// Test 5
const array_5 = [2,3,4,5,6];
const sorted_5 = quickSort(array_5);
const should_5 = [2,3,4,5,6];

document.querySelector('#sorted-5').textContent = sorted_5.toString();
document.querySelector('#should-5').textContent = should_5.toString();
const badge_5 = document.querySelector('#badge-5');
if (sorted_5.toString() === should_5.toString()) {
  badge_5.textContent = 'Passed';
} else {
  badge_5.textContent = 'Failed';

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.