C#快速排序

//快速排序的具体过程如下:

//1:在待排序的n个记录中,任取一个作为基准,把数组分为两部分,第一组的排序码都小于或等于该排序码,第二组的排序码,都大于或等于该排序码,并把该基准放在两组中间。

 2:采用同样的方法分别对左右两组进行排序,直到所有的记录都排到适当的位置。

///快速递归排序

        private static void QuickSort(int[] R, int low, int high)

        {

            int pivotLoc = 0;

            if (low < high)

            {

                pivotLoc = Partition(R, low, high);

                QuickSort(R, low, pivotLoc - 1);

                QuickSort(R, pivotLoc + 1, high);

            }

        }

        private static int Partition(int[] R, int low, int high)

        {

            int temp = R[low];

            while (low < high)

            {

                if (low < high && temp <= R[high])

                {

                    high--;

                }

                R[low] = R[high];

                if (low < high && R[low] <= temp)

                {

                    low++;

                }

                R[high] = R[low];

            }

            R[low] = temp;

            return low;

 

        }

 

        //快速非递归排序

        public static void QuickSort(int [] R,int Low,int High,Stack<int> stack)

        {

            int low = Low;

            int high = High;

            int temp = R[low];

            while (high > low)

            {

                while (low < high && temp <= R[high])

                {

                    high--;

                }

                if (high > low)

                {

                    R[low] = R[high];

                    R[high] = temp;

                }

                while (low < high && temp >= R[low])

                {

                    low++;

                }

                if (high > low)

                {

                    R[high] = R[low];

                    R[low] = temp;

                }

                if (Low < low - 1)

                {

                    stack.Push(Low);

                    stack.Push(low-1);

                }

                if (High > low + 1)

                {

                    stack.Push(low+1);

                    stack.Push(High);

                }

            }

        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值