using System;
using System.Collections.Generic;
using System.Text;

namespace SortAlgorithms
{
    class QuicktionSorter
    {
        private void Swap(ref int i, ref int r)
        {
            int temp;
            temp = r;
            r = i;
            i = temp;

        }

        public void Sort(int[] list, int low, int high)
        {
            int pivot;
            int i, r;
            int mid;
            if (high <= low)
            {
                return;
            }
            else if (high == low + 1)
            {
                if (list[low] > list[high])
                {
                    Swap(ref list[low],ref list[high]);
                }
                return;
            }
            mid = (low + high) >> 1;
            pivot = list[mid];
            Swap(ref list[low], ref list[mid]);
            i = low + 1;
            r = high;
            do
            {
              while(i<=r && list[i]<pivot)
              {
                i++;
              }
                while(list[r]>=pivot)
                {
                  r--;
                }
                if(i<r)
                {
                  Swap(ref list[i], ref list[r]);
                }
            }while(i<r);

            list[low] =list[r];
            list[r]=pivot;
            if(low+1<r)
            {
              Sort(list,low,r-1);
            }
            if(r+1<high)
            {
              Sort(list,r+1,high);
            }
            }

      

        //static void Main(string[] args)
        //{
        //    int[] arry = new int[] {3,4,2,5,34,3,6 };
        //    QuicktionSorter q = new QuicktionSorter();
        //    q.Sort(arry,0,arry.Length-1);
        //    foreach (int m in arry)
        //    {
        //        Console.WriteLine(m);
        //    }

        //    Console.ReadLine();
       
        //}
    }
}

posted on 2009-04-27 20:42  WQL.NET  阅读(169)  评论(0编辑  收藏  举报