After seeing this example of a "primitive" QS implementation in F# by Scott W
let rec quicksort2 = function | [] -> [] | first::rest -> let smaller,larger = List.partition ((>=) first) rest List.concat [quicksort2 smaller; [first]; quicksort2 larger]
He has an example in C# which is far longer, I figured that this approach should be able to be more closely modeled than his C# example so this was my best attempt:
private static IEnumerable<T> Quicksort<T>(IEnumerable<T> list)
where T : IComparable<T>
{
if ((list == null) || !list.Any())
return Enumerable.Empty<T>();
var smallerAndLarger = list.Skip(1).GroupBy(n => n.CompareTo(list.First()));
return Quicksort(smallerAndLarger.FirstOrDefault(x => x.Key < 0))
.Concat(new List<T>(new[] { list.First() }))
.Concat(Quicksort(smallerAndLarger.FirstOrDefault(x => x.Key >= 0)));
}
Now depending on how you count my solution is "equally" short as the F# one. But it does have more noise, that I cannot argue with. I wonder, can it be made clearer and perhaps even shorter than this?
.Concat(new List<T>(new[] { list.First() }))
to.Concat(new[] { list.First() })
\$\endgroup\$