• 我自己编写的实现的对一些列数字的索引进行排序的类 (注意:不是对数字集合进行排序,而是对数字集合的索引按照数字值进行排序)


    我自己编写的基于

    C#排序算法——基类的实现  http://blog.csdn.net/hustcyb/archive/2008/09/08/2899309.aspx

    实现的对一些列数字的索引进行排序的类  (注意:不是对数字集合进行排序,而是对数字集合的索引按照数字值进行排序)

    《IndexSorter.cs》

    using System;
    using System.Collections.Generic;
    using System.Text;


    namespace Taobao.open.api
    {
        /// <summary>
        /// 用于定义升序和降序的枚举
        /// </summary>
        public   enum  Order
        {
            ASC,
            DESC,
        }

        public   delegate   int  CompareDelegate<T>(T left, T right);

         /// <summary>
         /// 作者 : cyb
         /// 发表时间 : 2008-9-8
         /// qq : 13101908
         /// e-mail : hustcyb@gmail.com
         /// </summary>
        public abstract class IndexSorter
        {
            public virtual void IndexSort<T>(IList<int> listindex, IList<T> list, CompareDelegate<T> compare)
            {
                //  重载此函数,实现各种比较算法,但是客户端调用的时候调用的是
                //  IndexSorter<T>(IList<int> listindex, IList<T> list) where T : IComparable<T>
                //  或
                //  IndexSorter<T>(IList<int> listindex, IList<T> list) where T : IComparable<T>
                if  (list ==  null )
                {
                     throw new ArgumentNullException( "Argument for IList is null" );
                }

                if  (compare ==  null )
                {
                     throw new ArgumentNullException( "Argument for CompareDelegate is null" );
                }
            }

            // 以下2个函数为 IComparable
            public void IndexSort<T>(IList<int> listindex, IList<T> list, Order order) where T : IComparable<T>
            {
                CompareDelegate<T> compare;
                if  (order == Order.ASC)
                {
                    compare =  delegate (T first, T second)
                                {
                                     return  first.CompareTo(second);
                                };
                }
                else
                {
                    compare =  delegate (T first, T second)
                                {
                                     return  -first.CompareTo(second);
                                };
                }

                IndexSort(listindex, list, compare);
            }

            public void IndexSort<T>(IList<int> listindex, IList<T> list) where T : IComparable<T>
            {
                this.IndexSort(listindex, list, Order.ASC);
            }

             // 以下2个函数为 IComparer
            public void IndexSort<T>(IList<int> listindex, IList<T> list, IComparer<T> comparer, Order order)
            {
                CompareDelegate<T> compare;

                if  (order == Order.ASC)
                {
                    compare =  delegate (T first, T second)
                                {
                                     return  comparer.Compare(first, second);
                                };
                }
                 else
                {
                    compare =  delegate (T first, T second)
                                {
                                     return  -comparer.Compare(first, second);
                                };
                }

                IndexSort(listindex, list, compare);
            }

            public void IndexSort<T>(IList<int> listindex, IList<T> list, IComparer<T> comparer)
            {
                this.IndexSort(listindex, list, comparer, Order.ASC);
            }

            /// <summary>
            /// 交换集合中的两个元素,子类中会用到
            /// </summary>
            public static void SwapListItem(IList<int> listindex, int firstIndex, int secondIndex)
            {
                int temp = listindex[firstIndex];
                listindex[firstIndex] = listindex[secondIndex];
                listindex[secondIndex] = temp;            //T temp = list[firstIndex];
                //list[firstIndex] = list[secondIndex];
                //list[secondIndex] = temp;
            }
        }
    }

    《BubbleIndexSorter.cs》

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace Taobao.open.api
    {
         /// <summary>
         /// 作者 : 冒泡排序
         /// 发表时间 : 2008-9-8
         /// qq : 13101908
         /// e-mail : hustcyb@gmail.com
         /// </summary>
        public class BubbleSorter : IndexSorter
        {
            //public override void IndexSort<T>(IList<T> list, CompareDelegate<T> compare)
            public override void IndexSort<T>(IList<int> listindex, IList<T> list, CompareDelegate<T> compare)
            {
                for (int endIndex = list.Count - 1; endIndex > 0; endIndex--)
                {
                    for (int index = 0; index < endIndex; index++)
                    {
                        if (compare(list[listindex[index]], list[listindex[index + 1]]) > 0)   //list[listindex[index]] > list[listindex[index + 1]]
                        {
                            SwapListItem(listindex, index, index + 1);        //调用基类的SwapListItem方法交换list[index]和list[index + 1]
                        }
                    }

                    foreach (int i in listindex)
                    {
                        Console.Write(list[i] + " ");
                    }
                    Console.Write("\r\n");

                    int a = 1;
                }
            }
        }
    }

  • 相关阅读:
    Android:TabHost导航栏
    java:StringUtil工具类
    Android进阶篇MediaPlayer
    Android:图片滚轮
    Android:EditText焦点触发布局隐藏以及显示
    Android:Spinner的使用
    ASP.NET编程模型的理解
    ASP.NET页面事件(页面生命周期)
    根据用户喜欢的爱好选择不同风格CSS(ViewState)
    ASP.NET的页面指令
  • 原文地址:https://www.cnblogs.com/carl2380/p/2002403.html
Copyright © 2020-2023  润新知