三文鱼海域

Fools learn nothing from wise men, but wise men learn much from fools.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

堆排序:

  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    }
posted on 2007-06-22 19:12  三文鱼  阅读(554)  评论(0编辑  收藏  举报