C#冒泡泡算法

代码如下:

static void Main(string[] args)
        {
            int[] arr = new int[] { 87, 85, 89, 84, 76, 82, 90, 79, 78, 68 };//定义一个一维数组并赋值
            Console.WriteLine("初始序列:");
            foreach(int m in arr)   //循环遍历定义的一维数组,并输出其中的元素
                Console.WriteLine(m+" ");
            Console.WriteLine();
            //定义两个int类型的变量,分别用来表示数组下标和存储新的数组元素
            int j, temp;
            for (int i = 0; i < arr.Length;i++ )//根据数组下标的值遍历数组元素
            {
                j = i + 1;
            id:
                if (arr[i] > arr[j])  //判断前后两个数的大小
                {
                    temp = arr[i];  //将比较后大的元素赋值给定义的int变量
                    arr[i] = arr[j];//将后一个元素的值赋值给前一个元素
                    arr[j] = temp;  //将int变量中存储的元素赋值给后一个元素
                    goto id;        //返回标识
                }
                else                
                    if (j<arr.Length -1)
                    {
                        j++;
                        goto id;
                    }                
            }
            Console.WriteLine("排序后的序列:");
            foreach (int n in arr)
            {
                Console.WriteLine(n+" ");
                Console.ReadLine();
            }
        }

原文地址:https://www.cnblogs.com/myesn/p/5601617.html