冒泡排序

class Program17
    {
        public static void PopSort(int[] list)
        {
            int i, j, temp;  //先定义一下要用的变量
            for (i = 0; i < list.Length - 1; i++)
            {
                for (j = i + 1; j < list.Length; j++)
                {
                    if (list[i] > list[j]) //如果第二个小于第一个数
                    {
                        //交换两个数的位置,在这里你也可以单独写一个交换方法,在此调用就行了
                        temp = list[i]; //把大的数放在一个临时存储位置
                        list[i] = list[j]; //然后把小的数赋给前一个,保证每趟排序前面的最小
                        list[j] = temp; //然后把临时位置的那个大数赋给后一个
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            //string aa = "fdafd";
            //Console.WriteLine(aa.Length);
            int[] intBox = new int[] { 1,32,43,21,34,65,78,9};
            PopSort(intBox);
            foreach (int i in intBox)
            {
                Console.WriteLine(i);
            }
           // Console.WriteLine("int数组的长度:"+intBox.Length);
            Console.ReadLine();
        }
    }
原文地址:https://www.cnblogs.com/25miao/p/9879498.html