Skip to content

Instantly share code, notes, and snippets.

@mdmen
Last active April 16, 2018 17:23
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 mdmen/cf8d728cfacb4b860c55030f0b2052a5 to your computer and use it in GitHub Desktop.
Save mdmen/cf8d728cfacb4b860c55030f0b2052a5 to your computer and use it in GitHub Desktop.
Bubble sort in ES6
/**
* Bubble sort
* @param {array} arr - Array of numbers
*/
const bubbleSort = (arr = []) => {
const length = arr.length;
for (let i = 0; i < length; i++) {
for (let j = 0; j < length - i; j++) {
const tmp = arr[j + 1];
if (tmp < arr[j]) {
arr[j + 1] = arr[j];
arr[j] = tmp;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment