zoukankan      html  css  js  c++  java
  • 获取指定数量的有序列表

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

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

     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
  • 相关阅读:
    使用HandyJSON导致的内存泄漏问题相关解决方法
    iOS开发中获取视图在屏幕上显示的位置
    颜色框架Hue使用方法
    网络库Alamofire使用方法
    iOS开发中使用文字图标iconfont
    UISearchBar的扩展使用
    cocoapods导入第三方库提示RPC failed curl 18 transfer
    APP在模拟器崩溃,提示__gcd_queue_item_enqueue_hook_block_invoke
    APP崩溃提示:This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
    crm
  • 原文地址:https://www.cnblogs.com/lzzhang/p/4800754.html
Copyright © 2011-2022 走看看