Created
October 10, 2021 11:47
-
-
Save uchm4n/6158dff4e83a19674df29a51dcbebfc0 to your computer and use it in GitHub Desktop.
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
function StalinSort(array) { | |
if (!Array.isArray(array)) throw new TypeError('Argument must be an Array!'); | |
return array.reduce((prev, next) => | |
!prev.length || | |
next >= prev[prev.length - 1] ? | |
[...prev, next] : | |
prev | |
, []); | |
} | |
module.exports = StalinSort | |
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
<?php | |
function StalinSort(array $xs): array | |
{ | |
if (count($xs) <= 1) { | |
return $xs; | |
} | |
$zs = $xs; | |
$x = array_shift($zs); | |
$y = array_shift($zs); | |
if ($x <= $y) { | |
return [$x, ...sort([$y, ...$zs])]; | |
} | |
return StalinSort([$x, ...$zs]); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment