• [原创]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

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

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

  • 相关阅读:
    解决Prism的EventAggregator的事件订阅错误
    解决Prism中Region的GetView不起作用问题
    怎样成为一个高手
    排球比赛积分规则
    个人作业
    个人作业
    会议总结
    个人总结
    本周冲刺
    本周个人总结
  • 原文地址:https://www.cnblogs.com/virusswb/p/889408.html
Copyright © 2020-2023  润新知