Last active
October 7, 2019 04:44
-
-
Save thexdev/586f6e9a1cd5a2a8bdb3c96cc40fbb2b to your computer and use it in GitHub Desktop.
Bubble sorting algorithm in JavaScript
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 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