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

推荐订阅源

M
MIT News - Artificial intelligence
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
G
Google Developers Blog
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
月光博客
月光博客
B
Blog
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
博客园_首页
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Jina AI
Jina AI
S
SegmentFault 最新的问题
H
Help Net Security
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News

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

        }

    }

}