Tiniest bubble sort on the net?

jpantunes

JP Antunes

Posted on April 26, 2020

Tiniest bubble sort on the net?

I stumbled on an older dev.to post by the great Greg Bullmash that I really liked because it triggered my code-golfing nerve, and I figured I could come up with a bubble sort one-liner!

Turns out I was wrong, I need at least 4 3 lines to make tiniest working bubble sort algo on the net* but it was still a fun exercise so I'm sharing it.

const bubblie = (arr, swaps = false) => {
  arr.forEach((e,i) => { e > arr[i+1] ? ([arr[i], arr[i+1]] = [arr[i+1], arr[i]], swaps = true) : false });
  return !swaps ? arr : bubblie(arr, false);
}

*citation needed

💖 💪 🙅 🚩
jpantunes
JP Antunes

Posted on April 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related