快速排序算法(仅供个人拾遗)

 

百度百科:http://baike.baidu.com/view/19016.htm

 

C# 测试代码:

private static void Main(string[] args)
        {
            int[] data = new int[10];
            Random rdm = new Random();
            for (int i = 0; i < 10; i++)
            {
               
                data[i] = rdm.Next(1, 100);
            }
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(data[i]);
            }
            Console.WriteLine("");
            QuickSort.Sort(data,0,data.Length-1);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(data[i]);
            }

        }

 

 

public static class QuickSort
    {
        public static void Sort(int[] data, int low,
             int high)
        {

            int middle = data[(low + high) / 2];
            int i = low;
            int j = high;

            do
            {
                while (data[i] < middle && i < high)
                {
                    i++;
                }
                while (data[j] > middle && j > 0)
                {
                    j--;
                }
                if (i <= j)
                {
                    int temp = data[i];
                    data[i] = data[j];
                    data[j] = temp;
                    i++;
                    j--;
                }
            } while (i <= j);

            if (j > low)
            {
                Sort(data, low, j);
            }

            if (i < high)
            {
                Sort(data, i, high);
            }

        }
    }

 

posted @ 2014-12-09 14:53  zzhi.wang  阅读(236)  评论(0编辑  收藏  举报