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

    我自己编写的基于

    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;
                }
            }
        }
    }

  • 相关阅读:
    python 实例方法、静态方法、类方法的区别
    locust 参数化实现
    Airtest 基于图像识别的自动化测试工具
    python 调用 dubbo 接口
    locust+geventhttpclient 性能优化
    python性能测试工具locust
    性能测试工具 wrk
    jmeter 参数化,关联参数,断言等使用说明
    Django上传excel表格并将数据写入数据库
    小程序 wx.uploadFile 上传文件 iOS 失败 400 错误排查
  • 原文地址:https://www.cnblogs.com/carl2380/p/2002403.html
Copyright © 2011-2022 走看看