Skip to content

Instantly share code, notes, and snippets.

@thexdev
Last active October 7, 2019 04:44
Show Gist options
  • Save thexdev/586f6e9a1cd5a2a8bdb3c96cc40fbb2b to your computer and use it in GitHub Desktop.
Save thexdev/586f6e9a1cd5a2a8bdb3c96cc40fbb2b to your computer and use it in GitHub Desktop.
Bubble sorting algorithm in JavaScript
/**
* Bubble sorting algorithm in JavaScript.
*
* Here i'm using arrow function and ES6, but you can use native function and ES5 instead.
*
* @param {array} arr Unsorted Array
* @return {array}
*/
const bubbleSort = arr => {
let swapped;
do {
swapped = false;
for (let index = 0; index < arr.length; index++) {
if (arr[index] > arr[index + 1]) {
let tmp = arr[index];
arr[index] = arr[index + 1];
arr[index + 1] = tmp;
swapped = true;
}
}
} while (swapped);
return arr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment