设为首页 加入收藏

TOP

实现泛型数组的冒泡排序算法 C#
2019-09-02 23:55:29 】 浏览:12
Tags:实现 冒泡 排序 算法
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);
        }
    }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇捕获程序未抛出的异常 下一篇多线程编程学习笔记——使用并发..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目