LINQ QuickSort in C#

This is most certainly not the recommended way of implementing QuickSort, it assumes there are no duplicates, but it should work:

  1.         private IList<int> sort(IList<int> a)
  2.         {
  3.             if (a.Count <= 1)
  4.                 return a;
  5.             return sort(a.Where(x => x < a[0]).ToList()).Union(new List<int> { a[0] }).Union(sort(a.Where(x => x > a[0]).ToList())).ToList();
  6.         }

It's a nice demonstration of how C# is getting closer to resemble a proper lambda-oriented functional language.



Hah. Nice to know there are other people that do too many things in LINQ one liners. :P

Add new comment