var arr = [7,3,28,8,9,13,1500,45];
function qsort(a) {
if (a.length == 0) return [];
var left = [], right = [], pivot = a[0];
for (var i = 1; i < a.length; i++) {
a[i] < pivot ? left.push(a[i]) : right.push(a[i]);
}
return qsort(left).concat(pivot, qsort(right));
}
alert(qsort(arr));
This routine sorts array by using Quicksort algorithm.
The Question is how will the base case if (a.length == 0) return [];
ever be true to stop the recursion?