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

 

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4
  5 namespace  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值