冒泡排序

冒泡排序是比较常见的算法之一,也算是大家经常在面试中遇到的算法之一,算法的命名我们就不老生常谈了,直接进入正题。

冒泡排序其实也算是一种位置排序,在原本的数列An中,假设我们需要将数列An进行从小到大排序,那么我们可以通过以下方法:

1. 第一次遍历N(或者N-1 因为冒泡排序采用的是两两比较交换的原则,所以最后一个和倒数第二个比较完成就可得出结论,有没有这个- 1 取决于你选取的比较方法,当然选取两两交换的比较方法的有点不仅如此)个数据,找到最大的数据放在队列最后

2. 此时得到的数据是 An - 1   与  An的最大子项  即 An = (An-1) +  An的最大子项

3. 将An-1 当作新的数列,重复1的方法。

 

代码如下:

namespace BubbleSort
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            List<int> array = new List<int>() { 1, 12, 23, 44, 555, 666, 54, 23, 546, 45, 23, 45, 76, 87, 99, 33, 22, 454, 123, 2345, 77 };

            BubbleSort(array, true);
            ConsoleExtension.ListWriteLine<int>(array);
            BubbleSort(array, false);
            ConsoleExtension.ListWriteLine<int>(array);

            Console.ReadLine();
        }

        private static void BubbleSortAsc(List<int> source)
        {
            for (int i = 0; i < source.Count - 1; i++)
            {
                for (int j = 0; j < source.Count - 1 - i; j++)
                {
                    if (source[j] > source[j + 1])
                    {
                        var temp = source[j];
                        source[j] = source[j + 1];
                        source[j + 1] = temp;
                    }
                }
            }
        }


        private static void BubbleSortDesc(List<int> source)
        {
            for (int i = 0; i < source.Count - 1; i++)
            {
                for (int j = 0; j < source.Count - 1 - i; j++)
                {
                    if (source[j] < source[j + 1])
                    {
                        var temp = source[j];
                        source[j] = source[j + 1];
                        source[j + 1] = temp;
                    }
                }
            }
        }

        public static void BubbleSort(List<int> source, bool desc)
        {
            if (desc)
            {
                BubbleSortDesc(source);
            }
            else
            {
                BubbleSortAsc(source);
            }
        }
    }

    public static class ConsoleExtension
    {
        public static void ListWriteLine<T>(List<T> source,bool isNeedError = false)
        {
            if (source != null)
            {
                for (int i = 0; i < source.Count; i++)
                {
                    Console.Write(source[i].ToString());
                    if (i < source.Count - 1)
                    {
                        Console.Write(",");
                    }
                    else
                    {
                        // 注意跨平台问题,c#使用跨平台方法避免在centos,windows下不一致
                        Console.Write(System.Environment.NewLine);
                    }
                }
            }
            else
            {
                if (isNeedError)
                {
                    throw new Exception("null of your source");
                }
            }
        }
    }
}

 

这里还可以考虑使用其他的比较方法,例如引入外部变量用于存储最小值,然后将最小值放在队列首,然后排出队列第一个,将剩下的部分视为新队列继续找最小值(选择排序)等等,在时间复杂度上都是O(n2)。

总而言之,这只是位置排序的一种。

posted on 2019-09-11 17:00  SZMD.ls.nct  阅读(171)  评论(0)    收藏  举报

导航