Created
April 16, 2018 18:00
-
-
Save mdmen/5fa980af20ed1bb9b1f80bc2a1a8ab1f to your computer and use it in GitHub Desktop.
Insertion sort is 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
/** | |
* Insertion sort | |
* @param {array} arr - Array of numbers | |
*/ | |
const insertionSort = (arr = []) => { | |
const length = arr.length; | |
for (let i = 1; i < length; i++) { | |
const item = arr[i]; | |
let j = i - 1; | |
while (j >= 0 && arr[j] > item) { | |
arr[j + 1] = arr[j]; | |
arr[j] = item; | |
j--; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment