Skip to content

Instantly share code, notes, and snippets.

@jairhumberto
Created February 16, 2022 15:32
Show Gist options
  • Save jairhumberto/d825d794e6aa02c53bac28b0c904123f to your computer and use it in GitHub Desktop.
Save jairhumberto/d825d794e6aa02c53bac28b0c904123f to your computer and use it in GitHub Desktop.
My implementation of Bubble sort
function bubbleSort(array) {
for (let i=1, sorted; !sorted; i++) {
sorted = 1;
for (let j=0; j < array.length - i; j++) {
if (array[j] > array[j+1]) {
[array[j+1], array[j], sorted] = [array[j], array[j+1], 0];
}
}
}
}
@jairhumberto
Copy link
Author

Just for academic purposes.

Using the nifty Javascript Destructuring assignment!

Before someone, inadvertently points out, this is an already "optimized bubble sort" algorithm. The i variable of the first for, starts with value 1 to prevent that already ordered items be evaluated again through this section j < array.length - i, in the second for.

I believe this is the best implementation of Bubble sort so far. You can disagree though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment