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

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - 三文鱼

叶问 弧线枪法 我是传奇 赤壁~之战 也说动物园展示蟒蛇吃活鸡 终于知道g9是谁了 人生的蝴蝶效应 杭州之行 广州车展 西安之行 变形金刚真是火 C#写的几个基本的排序算法(三) C#写的几个基本的排序算法(二) 数据库惊现"NULL" 换了个计数器 应该了解的垃圾收集机制(二) 应该了解的垃圾收集机制(一) 中国队输了 别把代码写成“密码”
C#写的几个基本的排序算法(一)
三文鱼 · 2007-06-22 · via 博客园 - 三文鱼

写几个排序算法暖暖手,供参考。

堆排序:

  1public class HeapSortSample
  2    {
  3        public enum Relation
  4        {
  5            Parent,
  6            Left,
  7            Right
  8        }

  9
 10        public long GetRelation(long i, Relation r)
 11        {
 12            if (i < 0)
 13            {
 14                throw new ArgumentOutOfRangeException();
 15            }

 16
 17            long index = 0;
 18            switch (r)
 19            {
 20                case Relation.Parent:
 21                    index = (i + 1/ 2 - 1;    //父节点
 22                    break;
 23                case Relation.Left:
 24                    index = 2 * (i + 1- 1;    //左子节点
 25                    break;
 26                case Relation.Right:
 27                    index = 2 * (i + 1);    //右子节点
 28                    break;
 29                default:                    
 30                    break;
 31            }

 32            return index;
 33        }

 34
 35        //假设index的左右都是最大堆,加入index并形成一个新的最大堆
 36        public void MaxHeap(int[] values, long index, long heapSize)
 37        {
 38            if (index >= heapSize)
 39            {
 40                throw new ArgumentOutOfRangeException();
 41            }

 42
 43            long left = GetRelation(index, Relation.Left);
 44            long right = GetRelation(index, Relation.Right);
 45            long max = index;
 46            if (left < heapSize && values[index] < values[left])
 47            {
 48                max = left;
 49            }

 50            if (right < heapSize && values[max] < values[right])
 51            {
 52                max = right;
 53            }

 54
 55            if (max != index)
 56            {
 57                CommonFunctions<int> functions = new CommonFunctions<int>();
 58                functions.ExchangValues(ref values[max], ref values[index]);    //交换两个元素
 59
 60                MaxHeap(values, max, heapSize);
 61            }
            
 62        }

 63
 64        //创建一个最大堆
 65        public void BuildMaxHeap(int[] values)
 66        {
 67            long begin = values.Length / 2 - 1;
 68            for (long i = begin; i >= 0; i--)
 69            {
 70                MaxHeap(values, i, values.Length);
 71            }

 72        }

 73
 74        /// <summary>
 75        /// 对堆进行排序,执行完毕后heap变为升序的一个数组
 76        /// </summary>
 77        /// <param name="heap">最大堆</param>

 78        public void Sort(int[] heap)
 79        {            
 80            CommonFunctions<int> f = new CommonFunctions<int>();
 81            long length = heap.Length;
 82            while (length > 1)
 83            {
 84                f.ExchangValues(ref heap[0], ref heap[length - 1]); 
 85                length--;
 86                MaxHeap(heap, 0, length);
 87            }

 88        }
       
 89
 90        static void Main()
 91        {
 92            int[] values = 2549173860 };                       
 93
 94            HeapSortSample sort = new HeapSortSample();
 95            sort.BuildMaxHeap(values);
 96
 97            sort.Sort(values);
 98
 99            foreach (int v in values)
100            {
101                Console.Write(v + " ");
102            }

103        }

104    }