惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
罗磊的独立博客
腾讯CDC
云风的 BLOG
云风的 BLOG
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
U
Unit 42
I
InfoQ
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
美团技术团队
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
GbyAI
GbyAI
S
SegmentFault 最新的问题

博客园 - yuejianjun

搜索结果点击情况 进行加权 java httpURL连接远程服务器并返回数据(httpurlconnection)(转) 实体类 topN 过滤词 Lucene的评分(score) 位图求交集 位运算包含功能 页面抓取匹配时,万恶的 , , 要先替换掉为空,出现匹配有问题,都是这个引起的 探索推荐引擎内部的秘密,第 1 部分: 推荐引擎初探 深度用户行为 多维度深入分析笔记 lucene 搜索学习笔记 - OK 中文自动摘要提取 常用的vs编码 快捷键 Lucene中的堆(Heap)[ScorerDocQueue,TopScoreDocCollector] lucene 大数据量 快速 排序 T a s k 搜 索 ( L u c e n e ) l u c e n e 创 建 修 改 删 除 索 引 集中、分布式搜索引擎的4种设计方案 lucene fenlei ThreadPool 使用
topN 堆排序 (int 类型)
yuejianjun · 2012-06-26 · via 博客园 - yuejianjun

2012-06-26 15:00  yuejianjun  阅读(438)  评论()    收藏  举报

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics; 

namespace Test

{

    class Program

    {

        static void Main(string[] args)

        {

            List<int> testValues = new List<int>();

            Random rand = new Random();  

            int count = 100;

            for (int i = 0; i < count; i++)

            {

                testValues.Add(rand.Next()); 

            }

           int[] testArr = new int[testValues.Count];

           testValues.CopyTo(testArr);

           Stopwatch sw = new Stopwatch();

           sw.Reset();

           sw.Start();

           TopSort(testArr, 5); 

           sw.Stop();

           Console.WriteLine(count+" 耗时毫秒:"+sw.ElapsedMilliseconds);

           Console.Read();

        }

        public static void TopSort(int [] array, int top )

        { 

            //Judge input

            if (array.Length <= 2 || top >= array.Length / 2)

            {

                Array.Sort(array);

                return;

            }

            //One time partition

            int pivot = PartitionInt(array, 0, array.Length - 1, array.Length / 2);

            int lastPivot = pivot;

            //Run until pivot near the top

            while ((!(lastPivot >= top && pivot <= top)))

            {

                lastPivot = pivot;

                if (pivot > top)

                {

                    pivot = PartitionInt(array, 0, pivot, pivot / 2);

                    if (pivot == lastPivot)

                    {

                        pivot--;

                    }

                }

                else

                {

                    if (pivot >= array.Length - 1)

                    {

                        lastPivot = array.Length - 1;

                        break;

                    }

                    pivot = PartitionInt(array, pivot + 1, array.Length - 1, (array.Length - pivot) / 2);

                }

            }

            //Finally sort

            if (lastPivot < array.Length)

            {

                Array.Sort(array, 0, lastPivot + 1);

            }

            else

            {

                Array.Sort(array, 0, lastPivot);

            }

        }

        private static int PartitionInt(int[] array, int low, int high, int pivotIndex)

        {

            int pivotValue = array[pivotIndex];

            array[pivotIndex] = array[low];

            array[low] = pivotValue;

            while (low < high)

            {

                while (array[high] >= pivotValue && high > low)

                {

                    --high;

                }

                if (high > low)

                {

                    array[low] = array[high];

                }

                while (array[low] <= pivotValue && high > low)

                {

                    ++low;

                }

                if (high > low)

                {

                    array[high] = array[low];

                }

            }

            array[low] = pivotValue;

            return low;

        }

    }

}