获取指定数量的有序列表

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

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

 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

 

转载于:https://www.cnblogs.com/lzzhang/p/4800754.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值