欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实现泛型数组的冒泡排序算法

程序员文章站 2022-07-09 22:06:16
public static class BubbleSortTool { public static void BubbleSort(this T[] array, AscendingorDescending ascendingorDescending) where T:IComparable { ... ......
public static class BubbleSortTool
    {
        public static void BubbleSort<T>(this T[] array, AscendingorDescending ascendingorDescending) where T:IComparable
        {
            switch (ascendingorDescending)
            {
                case AscendingorDescending.ascending:
                    {
                        while (!SortCompleteChecK(array, AscendingorDescending.ascending))
                        {
                            for (int i = 0; i < (array.Length - 1); i++)
                            {
                                if (array[i].CompareTo(array[i + 1]) > 0)
                                {
                                    T temp = array[i];
                                    array[i] = array[i + 1];
                                    array[i + 1] = temp;
                                }
                            }
                        }
                    }
                    break;
                case AscendingorDescending.descending:
                    {
                        while (!SortCompleteChecK(array, AscendingorDescending.ascending))
                        {
                            for (int i = 0; i < (array.Length - 1); i++)
                            {
                                if (array[i].CompareTo(array[i + 1]) < 0)
                                {
                                    T temp = array[i];
                                    array[i] = array[i + 1];
                                    array[i + 1] = temp;
                                }
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        private static bool SortCompleteChecK<T>(T[] array, AscendingorDescending ascendingorDescending) where T : IComparable
        {
            switch (ascendingorDescending)
            {
                case AscendingorDescending.ascending:
                    {
                        for (int i = 0; i < (array.Length-1); i++)
                        {
                            if (array[i].CompareTo(array[i + 1]) >0  )
                            {
                                return false;
                            }
                        }
                        return true;
                    }
                    break;
                case AscendingorDescending.descending:
                    {
                        for (int i = 0; i < (array.Length - 1); i++)
                        {
                            if (array[i].CompareTo(array[i + 1]) < 0)
                            {
                                return false;
                            }
                        }
                        return true;
                    }
                    break;
                default:
                    { throw new Exception(); }
                    break;
            }

        }

        public enum AscendingorDescending
        {
            ascending,descending
        }

        public static void Print<T>(this T[] array)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                sb.Append(array[i]);
                sb.Append(" ");
            }
            Console.WriteLine(sb);
        }
        public static void Print<T>(this T[] array,String prompt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(prompt);
            for (int i = 0; i < array.Length; i++)
            {
                sb.Append(array[i]);
                sb.Append(" ");
            }
            Console.WriteLine(sb);
        }
    }