I think there could be a problem with the tests for this algorithm Implement Quick Sort: I wrote code that followed one of the solutions very closely. It works well on my own machine, but when I run it through FCC’s tests, I keep getting this error:
quickSort should not use the built-in .sort() method.
As you can see below, I am not using .sort():
if (array.length === 0) {
return [];
} else {
const pivotVal = array[0];
//sort elements into three arrays
let lesserArr = [];
let equalArr = [];
let greaterArr =[];
for (let e of array) {
if (e < pivotVal) {
lesserArr.push(e);
} else if (e > pivotVal) {
greaterArr.push(e)
} else {
equalArr.push(e);
}
}
array = [...quickSort(lesserArr), ...equalArr, ...quickSort(greaterArr)]
}
Thanks for the idea, but I thought the point here was to use recursion. If the function is called quickSort, and we are encouraged to use recursion, is not “sort” in the function inevitable?
Thanks. I understood what you meant the first time. My question is why would such a test be included, when the exercise is about recursion and the function is called quickSort(), which includes the word “sort”?
I removed any appearance of “sort” from my function, but it still does not pass the “sort()” test; and it works fine on my local machine. Here is my code:
function quickSort(array) {
// change code below this line
function mything(array) {
if (array.length === 0) {
return array;
} else {
const pivotVal = array[0];
//sort elements into three arrays
let lesserArr = [];
let equalArr = [];
let greaterArr =[];
for (let e of array) {
if (e < pivotVal) {
lesserArr.push(e);
} else if (e > pivotVal) {
greaterArr.push(e)
} else {
equalArr.push(e);
}
}
array = [...mything(lesserArr), ...equalArr, ...mything(greaterArr)]
}
// change code above this line
return array;
}
return mything(array);
}```
In code capitalization matter, the native method is called sort, so you can’t use the sort word (the hard ban was used to avoid renaming of the method and then be able to use it)
if you use Sort or any other capitalization that is not all lower case, then it’s fine
you still need to remove the comment containing the word sort