Created
June 9, 2018 10:13
-
-
Save kas-elvirov/a4328eeafb17fd79ce01de395e6d0586 to your computer and use it in GitHub Desktop.
JS implementation of bubble sorting 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 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