I'm attempting to program bubble sort recursively. This is my code so far:
function recursive_bubble_sort(array) {
if (array.length === 1) {
return array;
}
for (var i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
var temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
return [recursive_bubble_sort(array.splice(0, array.length - 1)), array[array.length -1]];
}
Technically it sorts the array but the return is not a one-dimensional array, instead it's in a reverse binary tree structure.
If the given array is [ 8, 2, 5, 1 ],
the output would be [ [ [ [ 1 ], 2 ], 5 ], 8 ].
How can I change the code so the output is like this: [ 1, 2, 5, 8 ]?