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));
此例程使用快速排序算法对数组进行排序。
问题是基本情况 if (a.length == 0) return [];
将如何停止递归?
最佳答案
if (a.length == 0) return [];
当长度为 0 时停止。
关于javascript - 在这种情况下递归如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6432469/