Hoping to get some help with this Quicksort algorithm in Javascript (it's not for homework or anything, just fun) - it's not working and I'm not sure where I'm going wrong.
function quicksort ( arr ) {
// Launch the sorting process.
sort(arr, 0, arr.length - 1 );
/**
* swap
* takes in an array and two indexes,
* swaps the elements in the array at those indexes
*/
function swap ( arr, a, b ) {
var temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
function partition ( arr, l, r) {
var p = arr[r],
i = l - 1,
j = l;
while ( j < r - 1) {
if (arr[j] <= p) {
swap ( arr, ++i, j );
}
j++;
}
swap (arr, i + 1, r);
return i + 1;
}
function sort ( arr, l, r ) {
var p;
if (l < r) {
p = partition( arr, l, r );
sort( arr, l, p - 1);
sort( arr, p + 1, r);
} else {
console.log(arr);
}
}
}