[C#] C# 实现堆排序算法代码 →→→→→进入此内容的聊天室

来自 , 2019-08-09, 写在 C#, 查看 109 次.
URL http://www.code666.cn/view/f7f07e7d
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5.  
  6. using Demo.Arithmetic.Sort;
  7.  
  8. namespace Demo.Arithmetic.DS
  9. {
  10.     /// <summary>
  11.     /// Represents a heap.
  12.     /// </summary>
  13.     /// <typeparam name="T">A comparable data structure.</typeparam>
  14.     public class Heap<T> where T : IComparable
  15.     {
  16.         /// <summary>
  17.         /// Heap type, either big root a small root.
  18.         /// </summary>
  19.         HeapType _heapType = HeapType.BigRoot;
  20.  
  21.         /// <summary>
  22.         /// An array contains the heap's data.
  23.         /// </summary>
  24.         T[] _rawData;
  25.  
  26.         /// <summary>
  27.         /// Constructor.
  28.         /// </summary>
  29.         /// <param name="capability">The heap's capability.</param>
  30.         /// <param name="heapType">The heap type.</param>
  31.         public Heap(int capability, HeapType heapType)
  32.         {
  33.             // Initializes the array.
  34.             _rawData = new T[capability];
  35.  
  36.             _heapType = heapType;
  37.         }
  38.  
  39.         #region Public methods
  40.  
  41.         /// <summary>
  42.         /// Sorts the array.
  43.         /// </summary>
  44.         /// <param name="sortOrder">Sort order.</param>
  45.         public void Sort(SortOrder sortOrder)
  46.         {
  47.             if (sortOrder == SortOrder.None) return;
  48.             if (_rawData == null || _rawData.Length <= 1) return;
  49.  
  50.             HeapType formerHeapType = _heapType;
  51.  
  52.             // If the order is ascending, then use big root, otherwise use small root.
  53.             _heapType = sortOrder == SortOrder.Ascending ? HeapType.BigRoot : HeapType.SmallRoot;
  54.  
  55.             // Build the heap up.
  56.             BuildHeap();
  57.  
  58.             for (int i = _rawData.Length - 1; i > 0; i--)
  59.             {
  60.                 Exchange(0, i);
  61.                 Heapify(0, i - 1);
  62.             }
  63.  
  64.             _heapType = formerHeapType;
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Build the heap up.
  69.         /// </summary>
  70.         public void BuildHeap()
  71.         {
  72.             for (int i = _rawData.Length / 2 - 1; i >= 0; i--)
  73.             {
  74.                 Heapify(i, _rawData.Length - 1);
  75.             }
  76.         }
  77.  
  78.         public void Heapify(int rootIndex, int lastIndex)
  79.         {
  80.             // If the root node is a leaf, then it is a heap already.
  81.             if (IsLeaf(rootIndex, lastIndex)) return;
  82.  
  83.             int nextRootIndex = GetRootIndex(rootIndex, lastIndex);
  84.  
  85.             if (nextRootIndex == rootIndex)
  86.             {
  87.                 return;
  88.             }
  89.             else
  90.             {
  91.                 // Exchange.
  92.                 Exchange(rootIndex, nextRootIndex);
  93.  
  94.                 // Verify the heap.
  95.                 Heapify(nextRootIndex, lastIndex);
  96.             }
  97.         }
  98.  
  99.         #endregion
  100.  
  101.         #region Private methods
  102.  
  103.         /// <summary>
  104.         /// Returns the root's index by compare the parent node and children nodes according to the heap's type.
  105.         /// </summary>
  106.         /// <param name="rootIndex">The parent's node index.</param>
  107.         /// <param name="lastIndex">The heap's last index.</param>
  108.         /// <returns>The root's index.</returns>
  109.         private int GetRootIndex(int rootIndex, int lastIndex)
  110.         {
  111.             int tLeftIndex = rootIndex + rootIndex + 1;
  112.             if (tLeftIndex > lastIndex) return rootIndex;
  113.  
  114.             int nextRootIndex = ((_heapType == HeapType.BigRoot && _rawData[rootIndex].CompareTo(_rawData[tLeftIndex]) > 0) ||
  115.                 (_heapType == HeapType.SmallRoot && _rawData[rootIndex].CompareTo(_rawData[tLeftIndex]) < 0)) ?
  116.                 rootIndex : tLeftIndex;
  117.  
  118.             int tRightIndex = rootIndex + rootIndex + 2;
  119.             if (tRightIndex > lastIndex) return nextRootIndex;
  120.  
  121.             nextRootIndex = ((_heapType == HeapType.BigRoot && _rawData[nextRootIndex].CompareTo(_rawData[tRightIndex]) > 0) ||
  122.                 (_heapType == HeapType.SmallRoot && _rawData[nextRootIndex].CompareTo(_rawData[tRightIndex]) < 0)) ?
  123.                 nextRootIndex : tRightIndex;
  124.  
  125.             return nextRootIndex;
  126.         }
  127.  
  128.         /// <summary>
  129.         /// Exchanges the nodes.
  130.         /// </summary>
  131.         /// <param name="firstIndex">The first node's index.</param>
  132.         /// <param name="secondIndex">The second node's index.</param>
  133.         private void Exchange(int firstIndex, int secondIndex)
  134.         {
  135.             T temp = _rawData[firstIndex];
  136.             _rawData[firstIndex] = _rawData[secondIndex];
  137.             _rawData[secondIndex] = temp;
  138.         }
  139.  
  140.         #endregion
  141.  
  142.         #region Static methods
  143.  
  144.         /// <summary>
  145.         /// Checks if the node is a leaf.
  146.         /// </summary>
  147.         /// <param name="nodeIndex">The node's index.</param>
  148.         /// <param name="lastIndex">The last index of the heap.</param>
  149.         /// <returns>Returns true if the node is a leaf.</returns>
  150.         private static bool IsLeaf(int nodeIndex, int lastIndex)
  151.         {
  152.             return nodeIndex + nodeIndex + 1 > lastIndex;
  153.         }
  154.  
  155.         #endregion
  156.  
  157.         #region Properties
  158.  
  159.         public T this[int i]
  160.         {
  161.             get
  162.             {
  163.                 return _rawData[i];
  164.             }
  165.  
  166.             set
  167.             {
  168.                 _rawData[i] = value;
  169.             }
  170.         }
  171.  
  172.         /// <summary>
  173.         /// The capability.
  174.         /// </summary>
  175.         public int Capability
  176.         {
  177.             get
  178.             {
  179.                 return _rawData.Length;
  180.             }
  181.         }
  182.  
  183.         #endregion
  184.     }
  185.  
  186.     /// <summary>
  187.     /// Heap type.
  188.     /// </summary>
  189.     public enum HeapType
  190.     {
  191.         BigRoot = 0x00,
  192.         SmallRoot = 0x01
  193.     }
  194. }
  195. //csharp/7235

回复 "C# 实现堆排序算法代码"

这儿你可以回复上面这条便签

captcha