I am trying to write a primitive quick sort code, with the pivot element in each recursion call being the first element of the array. I have completed my code and the recursion calls and partitions are correct but the final output is always the array obtained after the first partition call. Any idea what’s causing this problem?

```
function partition(arr, l, r) {
var p, i, j, swap = 0;
p = arr[l];
i = l + 1;
for (j = 1; j < arr.length; j++) {
if (arr[j] < p) {
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
i ++;
}
}
swap = arr[l];
arr[l] = arr[i - 1];
arr[i - 1] = swap;
return i - 1;
}
function quickSort(arr, n) {
var q, left, right;
if (arr.length > 1) {
q = partition(arr, 0, arr.length - 1);
console.log(q, arr[q]);
left = arr.slice(0, q);
right = arr.slice(q + 1, arr.length);
console.log(left, right);
quickSort(left, left.length);
quickSort(right, right.length);
}
}
array = [6, 4, 2, 3, 5, 7, 9, 8, 1];
quickSort(array, array.length);
console.log(array);
```

Thank you.