zoukankan      html  css  js  c++  java
  • 堆排序——算法系列

    堆排序:

    堆排序的思想,建立一个最大最小堆,通过不断取出堆顶的值,重建堆,再取出,得到排序。

    代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Net;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                for (int i = 1; i <= 5; i++)
                {
                    List<int> list = new List<int>();
                    //插入2k个随机数到数组中
                    for (int j = 0; j < 2000; j++)
                    {
                        Thread.Sleep(1);
                        list.Add(new Random((int)DateTime.Now.Ticks).Next(0, 100000));
                    }
                    Console.WriteLine("\n第" + i + "次比较:");
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    var result = list.OrderBy(single => single).ToList();//这里这个single=>single不懂
                    watch.Stop();
                    Console.WriteLine("\n系统的快速排序耗费时间:" + watch.ElapsedMilliseconds);
                    Console.WriteLine("输出前是十个数:"+string.Join(",",result.Take(10).ToList()));
                    watch.Start();
                    result = HeapSort(list);
                    watch.Stop();
                    Console.WriteLine("\n堆排序耗费时间:" + watch.ElapsedMilliseconds);
                    Console.WriteLine("输出前是十个数:" + string.Join(",", result.Take(10).ToList()));
                    
                }
                
                Console.ReadLine();
            }
            //堆排序算法
            static void HeapAdjust(List<int> list, int parent, int length)
            {
                int temp = list[parent];
                int child = 2 * parent + 1;
                while (child < length)
                {
                    if (child + 1 < length && list[child] < list[child + 1])
                        child++;
                    if (temp > list[child])
                        break;
                    list[parent] = list[child];
                    parent = child;
                    child = 2 * parent + 1;
                }
                list[parent] = temp;
            }
    
            public static List<int> HeapSort(List<int> list)
            {
                for (int i = list.Count / 2 - 1; i >= 0; i--)
                {
                    HeapAdjust(list, i, list.Count);
                }
    
                for (int i = list.Count - 1; i >= 0; i--)
                {
                    int temp = list[0];
                    list[0] = list[i];
                    list[i] = temp;
                    HeapAdjust(list, 0, i);
                }
                return list;
            }
        }
    }

    总结:堆排序一般的操作步骤就不说了,这里有一点要注意的就是,初始建堆的时候,是从叶子节点开始调整,一直到根,这样才能得到正确的堆。

  • 相关阅读:
    配合网页滚屏播放,做解说词
    @enable跟@import注解
    组合注解与元注解
    Spring Aware
    https的设计原理
    用信鸽来解释 HTTPS
    http三次握手四次挥手
    一致性哈希
    redis cluster原理
    redis cluster集群搭建
  • 原文地址:https://www.cnblogs.com/7ants/p/2955424.html
Copyright © 2011-2022 走看看