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

推荐订阅源

T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News | PayPal Newsroom
S
Security Affairs
T
Tor Project blog
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Help Net Security
Help Net Security
U
Unit 42
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
Cisco Talos Blog
Cisco Talos Blog
量子位
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
有赞技术团队
有赞技术团队
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
腾讯CDC
AI
AI
Last Week in AI
Last Week in AI
Latest news
Latest news
Google Online Security Blog
Google Online Security Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
V2EX - 技术
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 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;

        }

    }

}