zoukankan      html  css  js  c++  java
  • 三行代码实现快速排序

    一直对函数式编程的魔力所吸引,对λ演算来简化编程,对动态语言的特性有很浓厚的兴趣,也自学查找多方资料,现在列举一个我冥思苦想了好久才真正懂得的函数式的快速排序。

            /// <summary>
    /// 快速排序函数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <returns></returns>
    IEnumerable<T> QuickSorting<T>(IEnumerable<T> list) where T : IComparable<T>
    {

    if (list.Count() <= 1) return list;



    var pivot
    = list.First();

    return QuickSorting(list.Where(x => x.CompareTo(pivot) < 0))
    .Concat(list.Where(x
    => x.CompareTo(pivot) == 0))
    .Concat(QuickSorting(list.Where(x
    => x.CompareTo(pivot) > 0)));

    }

    测试方法:

            static void Main(string[] args)
    {
    List
    <int> ints = new List<int>
    {
    22,33,11,43,55,123,452,1,3,5,15,153,10000,532,553,22,33,11,53,11,33
    };
    List
    <int> lists = new Program().QuickSorting<int>(ints).ToList<int>();


    lists.ForEach(A
    => Console.WriteLine(A));
    }

    测试结果:

    正确…………!!

    亲爱的读者……你看懂了么……

    人有三等:第三流的人,一辈子看不透人性本质与商场的游戏则,他们皆深陷在错觉与梦幻中 第二流的人,是看透了,明白了,但是却不能自拔,甚至是无可奈何 第一流的人,是不仅看透想通,并且开始学会怎样玩这场游戏,用怎样的技巧来应对你的人生
  • 相关阅读:
    ERROR: epmd error for host "yourhostname": timeout
    leetcode485
    leetcode463
    leetcode496
    leetcode344
    leetcode412
    leetcode500
    leetcode476
    leetcode557
    leetcode461
  • 原文地址:https://www.cnblogs.com/TianMaiCheng/p/2154756.html
Copyright © 2011-2022 走看看