• 获取指定数量的有序列表


    信息系统中经常用到排序、查找等简单的数据结构,结合大数据量的后台设计实践;

    将插入排序、折半查找结合起来,实现一个经常用的场景:实时从海量数据中获取指定数量产品的有序列表

     1        /// <summary>
     2        /// 插入排序
     3        /// 获取前count个有序列表
     4        /// </summary>
     5        /// <param name="list"></param>
     6        /// <param name="count"></param>
     7        /// <returns></returns>
     8        public static List<decimal> GetSortCountList(List<decimal> list, int count = int.MaxValue)
     9        {
    10            var result = new List<decimal>();
    11            foreach (var item in list)
    12            {
    13                int index = GetOrderByIndex(result, item, count);
    14                if (index > -1)
    15                {
    16                    result.Insert(index, item);
    17                    if (result.Count > count)
    18                    {
    19                        result.RemoveAt(count);
    20                    }
    21                }
    22            }
    23            return result;
    24        }
    25 
    26         /// <summary>
    27         /// 插入排序
    28         /// 返回数值应该插入的index
    29         /// </summary>
    30         /// <param name="list"></param>
    31         /// <param name="price"></param>
    32         /// <returns></returns>
    33         public static int GetOrderByIndex(List<decimal> list, decimal price, int count = int.MaxValue)
    34         {
    35             int length = list.Count;
    36             if (length == 0)
    37             {
    38                 return 0;
    39             }
    40             else
    41             {
    42                 if (price >= list.Last())//大于等于最大
    43                 {
    44                     return length < count ? length : -1;
    45                 }
    46                 else if (price <= list.First()) //小于等于最小
    47                 {
    48                     return 0;
    49                 }
    50                 else//最小-->最大之间
    51                 {
    52                     return GetOrderByIndex(list, price, 1, length - 1);
    53                 }
    54             }
    55         }
    56 
    57         /// <summary>
    58         /// 折半查找
    59         /// 返回数值应该插入的index
    60         /// </summary>
    61         /// <param name="list"></param>
    62         /// <param name="item"></param>
    63         /// <param name="first"></param>
    64         /// <param name="last"></param>
    65         /// <returns></returns>
    66         public static int GetOrderByIndex(List<decimal> list, decimal item, int first, int last)
    67         {
    68             int middle = (last + first) / 2;
    69             var middlePrice = list[middle];
    70             if (last <= first)
    71             {
    72                 return (item < middlePrice) ? middle : middle + 1;
    73             }
    74             else
    75             {
    76                 if (item < middlePrice)
    77                 {
    78                     return GetOrderByIndex(list, item, first, middle - 1);
    79                 }
    80                 else
    81                 {
    82                     return GetOrderByIndex(list, item, middle + 1, last);
    83                 }
    84             }
    85         }
    View Code
  • 相关阅读:
    git常用操作命令
    如何编写高质量代码
    Chrome调试工具简单介绍
    使用eclipse+tomcat搭建本地环境
    chrome设置--disable-web-security解决跨域
    利用Maven管理工程项目本地启动报错及解决方案
    用户输入验证【提升篇】
    简单【用户输入验证】
    【消息框】的返回值
    【消息框】的4种显示形式
  • 原文地址:https://www.cnblogs.com/lzzhang/p/4800754.html
Copyright © 2020-2023  润新知