Skip to content

Instantly share code, notes, and snippets.

@thm-design
Created March 28, 2016 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thm-design/a452cd4c5ba1fc0ad32e to your computer and use it in GitHub Desktop.
Save thm-design/a452cd4c5ba1fc0ad32e to your computer and use it in GitHub Desktop.
Bubble Sort Algorithm
/*
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