-
-
Save foliveira/1051515 to your computer and use it in GitHub Desktop.
A lazy quick sort
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
public static IEnumerable<T> LazyQsort<T>(IEnumerable<T> seq, Func<T, T, bool> comp) | |
{ | |
if (seq.Any() == false) | |
{ | |
return Enumerable.Empty<T>(); | |
} | |
var pivot = seq.First(); | |
var xs = seq.Skip(1); | |
return LazyQsort(xs.Where(x => comp (x, pivot)), comp) | |
.Union(new[] { pivot }) | |
.Union(LazyQsort(xs.Where(x => !comp (x, pivot)), comp)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By using
Enumerable.Empty<T>()
you avoid a new initialisation everytimenew T[0]
gets called.