C# 冒泡排序算法

冒泡排序算法,就像它的名字一样生动形象,这个算法需要内外循环两次,外循环是控制整体的排序次数,外排次数=数组长度-1,关于这个也很好理解,比如10个数只要其他的9个位置确定了最后一个位置也就确定了,所以外排序的次数就是数组总个数减去1.内排序主要是控制每次的排序比较,之所以称之为冒泡就是因为每次排序的开始都是从数组的最后一位依次跟前一位比,就像气泡从水底冒出来一样,所以称之为冒泡排序

2,12,16,32,34,43,49,54,67,87

static void Main(string[] args)
        {
                int temp;
                int[] shuju = new int[10]{12,43,2,34,87,54,32,16,67,49};
                for (int i = 0; i < shuju.Length; i++)
                {
                    for (int j = i + 1; j < shuju.Length ; j++)
                    {
                        if (shuju[j] < shuju[i])
                        {
                            temp = shuju[j];
                            shuju[j] = shuju[i];
                            shuju[i] = temp;
                        }
                       
                    }
                }
                for (int j = 0; j < shuju.Length; j++)
                {
                 Console.Write("{0}"+" ",shuju[j]);
                }

               
            Console.ReadLine();
        }
         
            //(2)
            //static List<int> list = new List<int>() { 12, 43, 2, 34, 87, 54, 32, 16, 67, 49 };

            //static void Main(string[] args)
            //{
            //    Bubble();
            //    PrintList();
            //    Console.ReadLine();
            //}

            //static void Bubble()
            //{
            //    int temp = 0;
            //    for (int i = list.Count; i > 0; i--)
            //    {
            //        for (int j = 0; j < i - 1; j++)
            //        {
            //            if (list[j] > list[j + 1])
            //            {
            //                temp = list[j];
            //                list[j] = list[j + 1];
            //                list[j + 1] = temp;
            //            }
            //        }
            //        //PrintList();
            //        //Console.Write(string.Format("{0} ", list));
            //    }
            //}

            //private static void PrintList()
            //{
            //    foreach (var item in list)
            //    {
            //        Console.Write(string.Format("{0} ", item));
            //    }
            //    Console.WriteLine();
            //}

            //(3)
            //int[] arr = { 12, 43, 2, 34, 87, 54, 32, 16, 67, 49 };
            //int tmp = 0;
            //for (int i = 0; i < arr.Length - 1; i++)
            //{
            //    for (int j = 0; j < arr.Length - 1; j++)
            //    {
            //        if (arr[j] > arr[j + 1])
            //        {
            //            tmp = arr[j];
            //            arr[j] = arr[j + 1];
            //            arr[j + 1] = tmp;
            //        }
            //    }
            //}
            //for (int j = 0; j < arr.Length; j++)
            //{
            //    Console.Write("{0}  ", arr[j]);
            //}
            //Console.ReadLine();
        
    }
原文地址:https://www.cnblogs.com/iDennis/p/5779933.html