C# 集合擴充快速排序演算法

來源:互聯網
上載者:User

標籤:

  /// <summary>    /// 對集合進行排序,如    /// List<Person> users=new List<Person>(){.......}    /// ListSorter.SortList<list<Person>,Person>( users,"Age",SortDirection.Ascending);    /// </summary>    public static class ListUtil    {        public static void SortList<TCollection, TItem>(this TCollection list, string property, SortDirection direction) where TCollection : IList<TItem>        {            PropertyInfo[] propertyinfos = typeof(TItem).GetProperties();            foreach (PropertyInfo propertyinfo in propertyinfos)            {                if (propertyinfo.Name == property)          //取得指定的排序屬性                {                    QuickSort<TCollection, TItem>(list, 0, list.Count - 1, propertyinfo, direction);                }            }        }        /// <summary>        /// 快速排序演算法        /// </summary>        /// <typeparam name="TCollection">集合類型,需要實現Ilist<T>集合</typeparam>        /// <typeparam name="TItem">集合中對象的類型</typeparam>        /// <param name="list">集合對象</param>        /// <param name="left">起始位置,從0開始</param>        /// <param name="right">終止位置</param>        /// <param name="propertyinfo">集合中對象的屬性,屬性必須要實現IComparable介面</param>        /// <param name="direction">排序類型(升序或降序)</param>        private static void QuickSort<TCollection, TItem>(TCollection list, int left, int right, PropertyInfo propertyinfo, SortDirection direction) where TCollection : IList<TItem>        {            if (left < right)            {                int i = left, j = right;                TItem key = list[left];                while (i < j)                {                    if (direction == SortDirection.Ascending)                    {                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) < 0)                        {                            j--;                        }                        if (i < j)                        {                            list[i] = list[j];                            i++;                        }                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) > 0)                        {                            i++;                        }                        if (i < j)                        {                            list[j] = list[i];                            j--;                        }                        list[i] = key;                    }                    else                    {                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[j], null)) > 0)                        {                            j--;                        }                        if (i < j)                        {                            list[i] = list[j];                            i++;                        }                        while (i < j && ((IComparable)propertyinfo.GetValue(key, null)).CompareTo((IComparable)propertyinfo.GetValue(list[i], null)) < 0)                        {                            i++;                        }                        if (i < j)                        {                            list[j] = list[i];                            j--;                        }                        list[i] = key;                    }                }                //執行遞迴調用                 QuickSort<TCollection, TItem>(list, left, i - 1, propertyinfo, direction);                QuickSort<TCollection, TItem>(list, i + 1, right, propertyinfo, direction);            }        }    }    /// <summary>    /// 排序類型    /// </summary>    public enum SortDirection    {        Ascending,        Descending    }

 

static void SortList<TCollection, TItem>(params...)

static void QuickSort<TCollection, TItem>(params...) 

兩個函數摘自http://www.cnblogs.com/sosoft部落格,我直接將演算法擴充給了List<T>類

測試:

  static void  Main(){       Console.WriteLine("\r\n=======List<T>快速排序========");            List<Person> list2 = new List<Person>()            {                new Person { Name = "1", Age = 1 },                new Person { Name = "2", Age = 2 },                new Person { Name = "20", Age = 20 },                new Person { Name = "200", Age = 200 },                new Person { Name = "-1", Age = -1 },                new Person { Name = "-2", Age = -2 },                new Person { Name = "0", Age = 0 },            };            list2.SortList<List<Person>, Person>("Age", SortDirection.Descending);            foreach (var temp in list2)            {                Console.Write(temp.Name + " ");            }            Console.ReadLine();}  public  class Person    {        public string Name { get; set; }        public int Age { get; set; }    }

 

C# 集合擴充快速排序演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.