• [原创]c#快速排序类 Virus


    class QuickSort
        {
            private void Swap(ref int i, ref int j)
            {
                int t;
                t = i;
                i = j;
                j = t;
            }
            public void Sort(int[] list, int low, int high)
            {
                if (high <= low)
                {//only one element i array list
                    //so it do not need sort
                    return;
                }
                else if (high == low + 1)
                {
                    if (list[low] > list[high])
                    {
                        Swap(ref list[low], ref list[high]);
                        return;
                    }
                }
                myQuickSort(list, low, high);
            }
            private void myQuickSort(int[] list, int low, int high)
            {
                if (low < high)
                {
                    int pivot = Partition(list, low, high);
                    myQuickSort(list, low, pivot - 1);
                    myQuickSort(list, pivot + 1, high);
                }
            }
            private int Partition(int[] list, int low, int high)
            {
                int pivot;
                pivot = list[low];
                while (low < high)
                {
                    while(low <high&&list[high]>=pivot)
                    {
                        high--;
                    }
                    if(low!=high)
                    {
                        Swap(ref list[low], ref list[high]);
                        low++;
                    }
                    while(low<high&&list[low]<=pivot)
                    {
                        low++;
                    }
                    if(low!=high)
                    {
                        Swap(ref list[low], ref list[high]);
                        high--;
                    }
                }
                return low;
            }
        }

    class Program
        {
            static void Main(string[] args)
            {
                QuickSort quick = new QuickSort();
                int[] arr = new int[] {4,5,1,9,6,3,2,8,7 };
                quick.Sort(arr, 0, 8);
                foreach (int a in arr)
                {
                    Console.WriteLine(a);
                }
                Console.ReadLine();
            }
           
        }

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    拷贝构造函数 转型构造函数
    构造函数对const成员的初始化
    拷贝构造函数
    类数据成员和类成员函数
    顶层函数进行操作符重载与操作符重载直接作为类的构造函数
    构造函数和new 与new【】 析构函数
    Openfire+XIFF实时通信&一个特殊的GP
    客户端数据动态渲染
    数据处理:由面得点,构造其边界
    从GISALL博客搬家而来
  • 原文地址:https://www.cnblogs.com/virusswb/p/889408.html
Copyright © 2020-2023  润新知