Created
December 24, 2013 16:42
-
-
Save noblethrasher/8115437 to your computer and use it in GitHub Desktop.
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
static IEnumerable<T> QuickSort<T>(IList<T> xs) | |
where T : IComparable<T>, IEquatable<T> | |
{ | |
var stack = new Stack<Stack<T>>(new[] {new Stack<T>(xs)}); | |
while (stack.Any()) | |
{ | |
var qs = stack.Pop(); | |
var head = from q in qs where q.CompareTo(qs.Peek()) == 0 select q; | |
var less = from q in qs where q.CompareTo(qs.Peek()) < 0 select q; | |
var more = from q in qs where q.CompareTo(qs.Peek()) > 0 select q; | |
if (less.Any()) | |
stack.Push(new Stack<T>(less)); | |
if (more.Any()) | |
{ | |
stack.Push(new Stack<T>(head)); | |
stack.Push(new Stack<T>(more)); | |
} | |
else | |
foreach (var q in head) | |
yield return q; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment