分治法:用C#实现快速排序

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace QuickSort
  6{
  7    class Program
  8    {
  9        static void Main(string[] args)
 10        {
 11            int[] a = new int[] 4216360-511 };
 12            
 13            QuickSort q = new QuickSort();
 14            q.Init(a);
 15            q.Sort(0, a.Length - 1);
 16
 17            int[] r = q.GetResult();
 18
 19            if (r != null)
 20            {
 21                for (int i = 0; i < r.Length; i++)
 22                {
 23                    System.Console.WriteLine(r[i]);
 24                }

 25            }

 26        }
       
 27    }

 28
 29    class QuickSort
 30    {
 31        private int[] _toBeSort;
 32
 33        private bool _isSort;
 34
 35        public void Init(int[] toBeSort)
 36        {
 37            _isSort = false;
 38            _toBeSort = toBeSort;
 39        }

 40
 41        public int[] GetResult()
 42        {
 43            if (_isSort)
 44            {
 45                return _toBeSort;
 46            }

 47            else
 48            {
 49                return null;
 50            }

 51        }

 52
 53        public void Sort(int startIndex, int endIndex)
 54        {
 55            if (_toBeSort == null)
 56            {
 57                throw new Exception();
 58            }

 59
 60            if (endIndex > 1 + startIndex)
 61            {
 62                int part = partition(startIndex, endIndex);
 63
 64                //_toBeSort[part]已经在合适的位置上了,无需再次考虑。
 65                Sort(startIndex, part - 1);
 66                Sort(part + 1, endIndex);
 67            }

 68
 69            _isSort = true;
 70        }

 71
 72        private int partition(int startIndex, int endIndex)
 73        {
 74            int part = startIndex;
 75
 76            int mid = _toBeSort[startIndex];
 77
 78            int si = startIndex + 1;
 79            int bi = endIndex;
 80            int temp = 0;
 81
 82            while (si < bi)
 83            
 84                while (_toBeSort[si] < mid)
 85                {
 86                    si++;
 87                    if (si == endIndex)
 88                    {
 89                        break;
 90                    }

 91                }

 92
 93                while (_toBeSort[bi] > mid)
 94                {
 95                    bi--;
 96                }

 97
 98                if (si < bi)
 99                {
100                    temp = _toBeSort[bi];
101                    _toBeSort[bi] = _toBeSort[si];
102                    _toBeSort[si] = temp;
103                    si++;
104                    bi--;
105                }

106            }

107
108            temp = _toBeSort[bi];
109            _toBeSort[bi] = _toBeSort[startIndex];
110            _toBeSort[startIndex] = temp;
111
112            part = bi;
113
114            return part;
115        }

116    }

117
118    
119}

120
posted @ 2007-11-13 13:08 逖靖寒 阅读(393) 评论(5)  编辑 收藏 所属分类: 读书.NET 编程算法

  回复  引用  查看    
#1楼 2007-11-13 13:46 | egmkang      
lz有没有考虑过你这个快速排序的效率问题呢?
1000W个int数排完需要多少时间呢?
我写的快速排序需要2-3秒钟,MS类库里面的只需要1.5秒种,你的呢?
  回复  引用  查看    
#2楼 [楼主]2007-11-13 18:01 | 逖靖寒      
@egmkang
不好意思,我没有做过这个方面的测试。
你是怎么样得到1000W个int数的测试数据的啊?
请指教,谢谢
  回复  引用    
#3楼 2007-11-21 18:24 | Colorful [未注册用户]
这其实很容易获得,随机序列生成器。

但是楼主的这个递归算法是没办法获得1~3秒的性能,无他,递归很耗费时间,而且又是C#写的。

.NET中的Array.Sort(),看源码有好几种排序算法,不过它最经常用的还是调用C++的API,人家是无递归,而且有很好的优化。

楼主的算法优化也不够。
  回复  引用  查看    
#4楼 [楼主]2007-11-21 22:21 | 逖靖寒      
@Colorful
谢谢你的指教,我会仔细研究这个问题的。
  回复  引用  查看    
#5楼 2007-12-13 14:25 | egmkang      
@Colorful
这位老兄貌似很懂得样子,^_^
请教一个问题,我看Array.Sort() 里面的源码的时候发现一个TrySZSort()方法.这个方法是一个extern方法,具体指向哪里我就不知道了,能不能指点一下,或者是说说它调用了哪些C++API,谢谢啦