Skip to content

Instantly share code, notes, and snippets.

@uchm4n
Created October 10, 2021 11:47
Show Gist options
  • Save uchm4n/6158dff4e83a19674df29a51dcbebfc0 to your computer and use it in GitHub Desktop.
Save uchm4n/6158dff4e83a19674df29a51dcbebfc0 to your computer and use it in GitHub Desktop.
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
<?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