1

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 ]?

0

2 Answers 2

2

Here's a slightly different version, using destructuring instead of a temporary variable for the swap and the spread operator to build the output:

const recursive_bubble_sort = (arr) => {
  if (arr.length < 2) {return arr}

  for (let i = 0; i < arr.length - 1; i ++) {
    if (arr[i] > arr[i + 1]) {
      [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]
    }
  }

  return [... recursive_bubble_sort (arr.slice(0, -1)), ... arr.slice(-1)]
}

console .log (recursive_bubble_sort ([8, 2, 5, 1]))

That last line could also be

  return [... recursive_bubble_sort (arr.slice(0, -1)), arr[arr.length - 1]]

. It's simply an aesthetic call.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is the return statement

return [recursive_bubble_sort(array.splice(0, array.length - 1)), array[array.length -1]];

However, since recursive_bubble_sort either returns a single element recursive_bubble_sort or an array (the other return), you need to handle both.

One easy way is to just use Array#concat - if you give it a single element, it adds it to a the array, if you give it an array, it adds all elements, instead. This ensures the result is always flat:

You can instead

function recursive_bubble_sort(array) {
    if (array.length === 1) {
        return array[0];
    }

    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 [].concat(recursive_bubble_sort(array.splice(0, array.length - 1)), array[array.length -1]);
}

console.log(recursive_bubble_sort([ 8, 2, 5, 1 ]))

6 Comments

Thank you, concat() is what I was looking for, I tried to use push() but it didn't help.
Not really needed to use splice. slice will do the job without side effects.
You could also do return [...recursive_bubble_sort(array.slice(0, -1)), ...array.slice(-1)]; I find this cleaner than concat.
@ScottSauyet doesn't work because the first exit condition returns a simple item, and this would attempt to spread it. To succeed, the basic case should always wrap the value in an array but for me that's unnecessary.
Ah, I just copied the return from my own version, not noticing this change. But that points to another problem here, which is that sorting a one-element array returns a scalar value, not an array. I'll add a separate answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.