-
-
Save tcelovsky/a06e2222cc1e5e5d2f0156f54293c9b7 to your computer and use it in GitHub Desktop.
Bubble Sort Algorithm
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 sortItems(array) { | |
for (let i = 0; i < array.length; i++) { // for loop iterates through each item in the array; | |
for (let j = 0; j < array.length; j++) { // for loop makes comparisons between each element in the array; | |
if (array[j] > array[j + 1]) { // if statement checks if the number on the left of a comparison is greater than the number on the right; | |
let temp = array[j]; // if it is, we swap the numbers; otherwise, do nothing. | |
array[j] = array[j + 1]; | |
array[j + 1] = temp; | |
} | |
} | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment