Implementing quicksort algorithm

前端 未结 7 1281
走了就别回头了
走了就别回头了 2021-02-01 19:48

I found quicksort algorithm from this book

\"\"

This is the algorithm

QUICKSORT (A, p, r)
i         


        
相关标签:
7条回答
  • 2021-02-01 20:16

    This is the shortest implementation of Quick Sort algorithm (Without StackOverflowException)

    IEnumerable<T> QuickSort<T>(IEnumerable<T> i) where T :IComparable
    {
        if (!i.Any()) return i;
        var p = i.ElementAt(new Random().Next(0, i.Count() - 1));
        return QuickSort(i.Where(x => x.CompareTo(p) < 0)).Concat(i.Where(x => x.CompareTo(p) == 0)).Concat(QuickSort(i.Where(x => x.CompareTo(p) > 0)));
    }
    
    0 讨论(0)
提交回复
热议问题