Skip to content

Instantly share code, notes, and snippets.

@Tatsh
Last active July 14, 2020 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tatsh/d5e9c911f52722520dba2c61c375fa87 to your computer and use it in GitHub Desktop.
Save Tatsh/d5e9c911f52722520dba2c61c375fa87 to your computer and use it in GitHub Desktop.
Single-expression qsort() implementations
const sorted = (xs) =>
xs.length < 2
? xs
: sorted(xs.slice(1).filter((x) => x < xs[0])).concat(
[xs[0]],
sorted(xs.slice(1).filter((x) => x > xs[0]))
);
<?php
function sorted($xs) {
return count($xs) < 2 ? $xs : array_merge(sorted(array_filter(
array_slice($xs, 1),
function ($x) { return $x > $xs[0]; }
)
),
[$xs[0]],
sorted(array_filter(
array_slice($xs, 1),
function ($x) { return $x < $xs[0]; }
)
)
);
}
from typing import List, Union
def sorted2(xs: List[Union[float, int]]) -> List[Union[int, float]]:
return xs if len(xs) < 2 else sorted2(
list(filter(lambda x: x < xs[0], xs[1:]))) + [xs[0]] + sorted2(
list(filter(lambda x: x > xs[0], xs[1:])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment