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

推荐订阅源

博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
G
Google Developers Blog
B
Blog
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
The Cloudflare Blog
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
量子位
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
H
Help Net Security
Help Net Security
Help Net Security
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
N
News | PayPal Newsroom
AWS News Blog
AWS News Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
腾讯CDC
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
GbyAI
GbyAI
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker

博客园 - 三文鱼

叶问 弧线枪法 我是传奇 赤壁~之战 也说动物园展示蟒蛇吃活鸡 终于知道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    }