JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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>
<h3>
<span id="sorted-1" class="array">[]</span>
should be
<span id='should-1' class="array">[]</span>
</h3>
<hr />
<h2>Test #2 <span id="badge-2" class="badge"></span></h2>
<p>Should handle 0s and negative numbers.</p>
<h3>
<span id="sorted-2" class="array">[]</span>
should be
<span id='should-2' class="array">[]</span>
</h3>
<hr />
<h2>Test #3 <span id="badge-3" class="badge"></span></h2>
<p>Should handle even amount of elements.</p>
<h3>
<span id="sorted-3" class="array">[]</span>
should be
<span id='should-3' class="array">[]</span>
</h3>
<hr />
<h2>Test #4 <span id="badge-4" class="badge"></span></h2>
<p>Should handle duplicates.</p>
<h3>
<span id="sorted-4" class="array">[]</span>
should be
<span id='should-4' class="array">[]</span>
</h3>
<hr />
<h2>Test #5 <span id="badge-5" class="badge"></span></h2>
<p>Should handle duplicates.</p>
<h3>
<span id="sorted-5" class="array">[]</span>
should be
<span id='should-5' class="array">[]</span>
</h3>
<hr />
</div>
</div>
<h5 class="copyright">
© 2016 Jorge Cuadra
<a href="www.cuadranteweb.com">www.cuadranteweb.com</a>
</h5>
.container,
.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]] :
leftArray.slice(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';
badge_1.classList.add('passed');
} else {
badge_1.textContent = 'Failed';
badge_1.classList.add('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';
badge_2.classList.add('passed');
} else {
badge_2.textContent = 'Failed';
badge_2.classList.add('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';
badge_3.classList.add('passed');
} else {
badge_3.textContent = 'Failed';
badge_3.classList.add('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';
badge_4.classList.add('passed');
} else {
badge_4.textContent = 'Failed';
badge_4.classList.add('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';
badge_5.classList.add('passed');
} else {
badge_5.textContent = 'Failed';
badge_5.classList.add('failed');
}
Also see: Tab Triggers