选择排序中的快速排序

选择排序中的快速排序
namespace swapSort
{
    class Program
    {
        static void Main(string[] args)
        {
            sort sortTest = new sort(101);
            //sortTest.BubbleSort();
            sortTest.QuickSort(0,100);
            sortTest.myPrint();

        }

    }
    class sort
    {
        private int[] myArray;
        private int size;
        private static Random myTandom = new Random();
        private int temp;
        public sort(int size)
        {
            int i;
            this.size = size;
            myArray=new int[size];
            for (i = 0; i < size;i++)
            {
                myArray[i]=myTandom.Next(1,100);
            }
        }
        public void QuickSort(int low,int high)
        {
            int i = low, j = high;
            temp = myArray[i];
            while (i < j)
            {
                while (i < j && temp < myArray[j])j--;
                if (i < j)
                {
                    myArray[i] = myArray[j];
                    i++;
                }
                while (i < j && temp > myArray[i]) i++;
                if (i < j)
                {
                    myArray[j] = myArray[i];
                    j--;
                }
            }
            myArray[i] = temp;
            if (low < i) QuickSort(low, i - 1);
            if (i < high) QuickSort(j + 1, high);   //不要写到while循环的内部
        }
        public void myPrint()
        {
            for (int i = 0; i < size; i++)
            {
                Console.Write(" " + myArray[i]);
            }
        }
    }
}

posted on 2010-01-04 16:37  涂圣飞  阅读(88)  评论(0编辑  收藏  举报

导航