获取指定数量的有序列表

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

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

 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

 

posted @ 2015-09-11 14:03  格子左左  阅读(149)  评论(0编辑  收藏  举报