zoukankan      html  css  js  c++  java
  • [原创]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

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

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

  • 相关阅读:
    fedora 安装open office
    git rebase(转)
    javascript typeof
    正则表达式入门
    XML格式
    zz 通用线程:Awk 实例,第 3部分
    ELF BIN HEX
    i2c总线(iic总线/ I square C)
    grep
    把Nginx注册成Windows 系统服务(转载)
  • 原文地址:https://www.cnblogs.com/virusswb/p/889408.html
Copyright © 2011-2022 走看看