Implementing quicksort algorithm

前端 未结 7 1285
走了就别回头了
走了就别回头了 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 QuickSort(IEnumerable 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)));
    }
    

提交回复
热议问题