欢迎访问我的博客 https://javascript.shop

排序 快速排序

原文发布时间为:2009-03-06 —— 来源于本人的百度文章 [由搬家工具导入]

using System;//快速排序

namespace sorts
{
    class Class7
    {
        public static void Main()
        {
            int[] a = new int[] { 4, 5, 8, 4, 6, 8, 5, 7 };
            QuickSort(a, 0, a.Length - 1);
            for (int i = 0; i < a.Length; i++)
                Console.Write("{0} ", a[i]);
            Console.ReadLine();
        }

        public static void QuickSort(int[] a, int s, int t)
        {
            int i, j, tmp;
            i = s;
            j = t;
            if (s < t)
            {
                tmp = a[s];
                while (i != j)
                {
                    while (j > i && a[j] > tmp)
                        j--;
                    if (i < j)
                    {
                        a[i] = a[j];
                        i++;
                    }
                    while (i < j && a[i] < tmp)
                        i++;
                    if (i < j)
                    {
                        a[j] = a[i];
                        j--;
                    }
                }

                a[i] = tmp;
                QuickSort(a, s, i - 1);
                QuickSort(a, i + 1, t);

            }
        }
    }
}

posted @ 2017-07-12 00:05  孑孓子  阅读(109)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop