Created
January 3, 2021 11:15
-
-
Save darkcris1/262ec3ad6fa7fdfd8cd298836559e157 to your computer and use it in GitHub Desktop.
Snailsort Algorithm | Codewars
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
const snailSort = (arr) => { | |
let result = [] | |
while (arr.length) { | |
if (arr.length === 1) { | |
return [...result, ...arr[0]] | |
} | |
for (let i = 0; i < arr[0].length; i++) { | |
result.push(arr[0][i]) | |
} | |
arr.shift() | |
for (let j = 0; j < arr.length; j++) { | |
result.push(arr[j][arr[j].length - 1]) | |
arr[j].pop() | |
} | |
for (let k = arr[arr.length - 1].length - 1; k >= 0; k--) { | |
result.push(arr[arr.length - 1][k]) | |
} | |
arr.pop() | |
for (let l = arr.length - 1; l >= 0; l--) { | |
result.push(arr[l][0]) | |
arr[l].shift() | |
} | |
} | |
return result.filter((val) => val !== undefined) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment