Created
March 28, 2016 15:36
-
-
Save thm-design/a452cd4c5ba1fc0ad32e to your computer and use it in GitHub Desktop.
Bubble Sort Algorithm
This file contains hidden or 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
/* | |
Bubble sort works by comparing two adjacent numbers next to each other and then | |
swapping their places if the smaller index's value is larger than the larger | |
index's. Continue looping through until all values are in ascending order | |
*/ | |
const bubbleSort = (arr) => { | |
arr.forEach((n, i) => { | |
if (arr[i] > arr[i + 1]) { | |
arr[i] = arr[i + 1]; | |
arr[i + 1] = n; | |
bubbleSort(arr) | |
} | |
return arr; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment