快速排序算法c#版

来源:互联网 发布:韩顺平mysql视频 云盘 编辑:程序博客网 时间:2024/05/07 15:55

using System;
using System.Collections.Generic;

namespace QuickSort
{
    class Sort
    {
        static void Main(string[] args)
        {
            List<int> list=new List<int>();
            Console.WriteLine("快速排序法之后支点降序算法");
            int[] arr = new int[] { 55, 6, 4, 7, 22, 59, 859, 2, 3, 35, 65 };
            list.AddRange(arr);
            QuickSortDescend(list, 0, list.Count - 1);
            foreach (int i in list)
            {
                Console.WriteLine(i.ToString());
            }
            Console.ReadLine();
        }

        //快速排序算法前支点升序算法
        public static void QuickSortAscend(List<int> list, int low, int high)
        {
            if (high > low)
            {
                int idxPartition = QuickSortAscendPartition(list, low, high);
                QuickSortAscend(list, low, idxPartition - 1);
                QuickSortAscend(list, idxPartition + 1, high);
            }

        }     
        private static int QuickSortAscendPartition(List<int> list, int low, int high)
        {
            int pivot = list[low];    //支点位置处的值
            while (low < high)
            {
                //后边向前比较
                while (low < high && list[high] <= pivot)
                {
                    high--;
                }
                //检查位置
                if (low != high)
                {
                    list[low] = list[high];  //H处位置值较小,覆盖L处
                    low++;
                }
                //继续比较,L向右
                while (low < high && list[low] >= pivot)
                {
                    low++;
                }
                //检查位置
                if (low != high)
                {
                    list[high] = list[low];
                    high--;
                }
            }
            list[low] = pivot;
            return low;
        }


        //快速排序法之后支点降序算法
        public static void QuickSortDescend(List<int> list, int left, int right)
        {
            if ( right> left)
            {
                int idxPartition = QuickSortDescendPartition(list, left, right);
                QuickSortDescend(list, left, idxPartition - 1);
                QuickSortDescend(list, idxPartition + 1, right);
            }
        }
        private static int QuickSortDescendPartition(List<int> list, int left, int right)
        {

            int pivot = list[right];
            while (left < right)
            {
                while (left < right && list[left] >= pivot)
                {
                    left++;
                }
                if (left != right)
                {
                    list[right] = list[left];
                    right--;
                }
                while (left < right && list[right] <= pivot)
                {
                    right--;
                }
                if (left != right)
                {
                    list[left] = list[right];
                    left++;
                }
            }
            list[left] = pivot;
            return left;
        }
    }
}