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

推荐订阅源

Jina AI
Jina AI
Recent Announcements
Recent Announcements
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
WordPress大学
WordPress大学
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
I
Intezer
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Threat Research - Cisco Blogs
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
S
Securelist
Cyberwarzone
Cyberwarzone
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
T
Tor Project blog
Project Zero
Project Zero
腾讯CDC
Recorded Future
Recorded Future
Help Net Security
Help Net Security
Spread Privacy
Spread Privacy
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
L
Lohrmann on Cybersecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
N
News and Events Feed by Topic
D
DataBreaches.Net
V
Vulnerabilities – Threatpost
L
LangChain Blog
博客园 - 【当耐特】
V2EX - 技术
V2EX - 技术
Simon Willison's Weblog
Simon Willison's Weblog
Engineering at Meta
Engineering at Meta
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
D
Docker
C
CXSECURITY Database RSS Feed - CXSecurity.com
人人都是产品经理
人人都是产品经理

博客园 - yuejianjun

搜索结果点击情况 进行加权 java httpURL连接远程服务器并返回数据(httpurlconnection)(转) topN 堆排序 (int 类型) 过滤词 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
yuejianjun · 2012-06-26 · via 博客园 - yuejianjun

2012-06-26 16:22  yuejianjun  阅读(201)  评论()    收藏  举报

ObjectScore[] obj = ObjectScoreArray.GetObjectScoreArray(); 

ScoreSort.TopSort(obj, 5, new ObjectScoreComparer());

 obj 前5个最小

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Test

{

    class ScoreSort

    {

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

        { 

            //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, comparer);//前lastPivot + 1行是最小的,但他们顺序没排好,最后对前lastPivot + 1个进行排序

            }

            else

            {

                Array.Sort(array, 0, lastPivot, comparer);

            }

        }

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

        {

            ObjectScore pivotValue = array[pivotIndex];

            array[pivotIndex] = array[low];

            array[low] = pivotValue;

            while (low < high)

            {

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

                {

                    --high;

                }

                if (high > low)

                {

                    array[low] = array[high];

                } 

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

                {

                    ++low;

                }

                if (high > low)

                {

                    array[high] = array[low];

                }

            } 

            array[low] = pivotValue;

            return low;

        }

    }

    public class ObjectScore

    {

        public int ID { get; set; }

        public float  Score { get; set; }

    }

    public class ObjectScoreComparer : IComparer<ObjectScore>

    {

        public int Compare(ObjectScore x, ObjectScore y)

        {

            return x.Score.CompareTo(y.Score);

        }

    }

    public class ObjectScoreArray

    {

        /// <summary>

        /// 初始化数据集

        /// </summary>

        /// <returns></returns>

        public static ObjectScore[] GetObjectScoreArray()

        {

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

            Random rand = new Random();

            int count = 500000;

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

            {

                ObjectScore obj = new ObjectScore();

                obj.ID = i;

                obj.Score =float .Parse ( (rand.Next(3,1000) /4).ToString ());

                testValues.Add(obj);

            }

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

            testValues.CopyTo(testArr);

            return testArr;

        }

    }

}