Last active
April 16, 2018 17:23
-
-
Save mdmen/cf8d728cfacb4b860c55030f0b2052a5 to your computer and use it in GitHub Desktop.
Bubble sort in ES6
This file contains 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 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