QuickSort snippets

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace QuickSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] keys= { 8, 1, 4, 7, 3 ,23,0,4,97,-1};
            IComparer myComparer = new myReverserClass();

            QuickSort(keys, 0, keys.Length-1, myComparer);
            Console.WriteLine("结果是:");
            foreach(int i in keys){              
            Console.Write(i+"\t");
            }
            Console.Read();

        }

      
        internal static void QuickSort(int[] keys, int left, int right, IComparer comparer)
        {
            do
            {
                int a = left;
                int b = right;
                int temp = a + ((b - a) >> 1);
                Console.WriteLine("temp:" + temp + "\t a:" + a + "\t b:" + b);
                SwapIfGreaterWithItems(keys, comparer, a, temp);
                SwapIfGreaterWithItems(keys, comparer, a, b);
                SwapIfGreaterWithItems(keys, comparer, temp, b);
                int y = keys[temp];
                do
                {
                    while (comparer.Compare(keys[a], y) < 0)
                    {
                        a++;
                    }
                    while (comparer.Compare(y, keys[b]) < 0)
                    {
                        b--;
                    }
                    if (a > b)
                    {
                        break;
                    }
                    if (a < b)
                    {
                        int local2 = keys[a];
                        keys[a] = keys[b];
                        keys[b] = local2;
                    }
                    a++;
                    b--;
                    foreach (int j in keys) {
                        Console.Write(j+"\t");
                    } Console.WriteLine();
                }
                while (a <= b);
                if ((b - left) <= (right - a))
                {
                    if (left < b)
                    {
                       QuickSort(keys, left, b, comparer);
                    }
                    left = a;
                }
                else
                {
                    if (a < right)
                    {
                        QuickSort(keys, a, right, comparer);
                    }
                    right = b;
                }
            }
            while (left < right);
        }


        private static void SwapIfGreaterWithItems(int[] keys, IComparer comparer, int a, int b)
        {
            if ((a != b) && (comparer.Compare(keys[a], keys[b]) > 0))
            {
                int local = keys[a];
                keys[a] = keys[b];
                keys[b] = local;
            }
        }
    }
    public class myReverserClass : IComparer
    {
        int IComparer.Compare(Object x, Object y)
        {
            {
                return ((new CaseInsensitiveComparer()).Compare(y, x));
                //通过使用 CaseInsensitiveComparer 类将 ArrayList 中内容的顺序反转来实现 IComparer 接口
            }

        }

    }
}

 

posted @ 2010-01-30 12:21  Cutting.pht  阅读(83)  评论(0编辑  收藏