Here is the quicksort code I wrote. The function doesn't work because it can't reach the base case. If I log the pivot, r
and l
to the console, they remain the same no matter how many times the sort function is called. So I wonder if the argument l
, r
are not really passed into the function as data. Why did it happen?
function sort(data){
if(data.length < 2){
return data;
}
else{
var l = [];
var r = [];
var pivot = parseInt(data.length/2);
for(i=0; i<data.length; i++){
if(data[i] > data[pivot]){
r.push(data[i]);
}
else{
l.push(data[i]);
}
}
return sort(l).concat(sort(r));
}
}
originalArray.sort()
?l
andr
?