最近闲来无事,看了看之前的数据结构,温习下冒泡排序的实现,

其实冒泡排序主要使用大数沉底方法,每次至少找出一个最大值,放到最右边

算法时间复杂度(n*n)

 

class Program
    {
        static void Main()
        {
            Console.WriteLine("Please Enter Number List To Bubble Sort");
            string input = Console.ReadLine();
            while (string.IsNullOrEmpty(input))
            {
                Console.WriteLine("Please Enter Number List To Bubble Sort ");
                input = Console.ReadLine();
            }

            string[] inputstring = input.Split(',');
            int[] sortArray = new int[inputstring.Length];

            for (int i = 0; i < input.Split(',').Length; i++)
            {
                int outNumber;
                if (!int.TryParse(inputstring[i], out outNumber))
                {
                    Console.WriteLine("Only Number Can Be Input Please Check");
                    return;
                }
                sortArray[i] = outNumber;
            }

            for (int j = 0; j < BubbleSort(sortArray).Length; j++)
            {
                Console.Write(sortArray[j] + ",");
            }

            Console.ReadLine();

        }

        public static int[] BubbleSort(int[] sortArray)
        {
            for (int i = 0; i < sortArray.Length; i++)
            {
                for (int j = 0; j < sortArray.Length - i - 1; j++)
                {
                    if (sortArray[j] > sortArray[j + 1])
                    {
                        int temp = sortArray[j];
                        sortArray[j] = sortArray[j + 1];
                        sortArray[j + 1] = temp;
                    }
                }
            }
            return sortArray;
        }
    }