Skip to content

Instantly share code, notes, and snippets.

@kas-elvirov
Created June 9, 2018 10:13
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 kas-elvirov/a4328eeafb17fd79ce01de395e6d0586 to your computer and use it in GitHub Desktop.
Save kas-elvirov/a4328eeafb17fd79ce01de395e6d0586 to your computer and use it in GitHub Desktop.
JS implementation of bubble sorting algorithm
/**
* Bubble sorting function
*
* @param {array} array with numbers
*
* @return {array} sorted array
*/
const sort = array => {
let arr = Array.prototype.slice.call(array);
for (let i = 0, endI = arr.length - 1; i < endI; i++) {
let wasSwap = false;
for (let j = 0, endJ = endI - i; j < endJ; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
wasSwap = true;
}
}
if (!wasSwap) break;
}
return arr;
};
let array = [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92];
console.log(sort(array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment