Implementing quicksort algorithm

前端 未结 7 1406
走了就别回头了
走了就别回头了 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 19:57

    Just in case you want some shorter code for Quicksort:

        IEnumerable QuickSort(IEnumerable i)
        {
            if (!i.Any())
                return i;
            var p = (i.First() + i.Last) / 2 //whichever pivot method you choose
            return QuickSort(i.Where(x => x < p)).Concat(i.Where(x => x == p).Concat(QuickSort(i.Where(x => x > p))));
        }
    

    Get p (pivot) with whatever method is suitable of course.

提交回复
热议问题