Created
February 16, 2022 15:32
-
-
Save jairhumberto/d825d794e6aa02c53bac28b0c904123f to your computer and use it in GitHub Desktop.
My implementation of Bubble sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 firstfor
, starts with value1
to prevent that already ordered items be evaluated again through this sectionj < array.length - i
, in the secondfor
.I believe this is the best implementation of Bubble sort so far. You can disagree though.