Last active
July 10, 2022 16:05
-
-
Save helabenkhalfallah/8b1867e31f916344b3ae685b3e34176a to your computer and use it in GitHub Desktop.
Vintage Insertion Sort
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
const vintageInsertionSort = (data) => { | |
console.log('data input : ', data); | |
for (let i = 1; i < data.length; i++) { | |
console.log('data i : ', data); | |
// First, choose the element at index 1 | |
let current = data[i]; | |
let j; | |
// Loop from right to left, starting from i-1 to index 0 | |
for (j = i - 1; j >= 0 && data[j] > current; j--) { | |
// as long as data[j] is bigger than current | |
// move data[j] to the right at arr[j + 1] | |
data[j + 1] = data[j]; | |
} | |
// Place the current element at index 0 | |
// or next to the smaller element | |
data[j + 1] = current; | |
} | |
return data; | |
} | |
console.log(vintageInsertionSort([89, 13, 57, 44, 71, 51])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment