课堂随笔05--冒泡排序
//定义一个八位数组并赋值 int[] arr1 = new int[8]; Console.WriteLine("数组值依次为:"); for (int i = 0;i < arr1.Length;i++) { Random ra = new Random(); int j = ra.Next(100);//出一个随机数 arr1[i] = j;//将随机数赋给数组值 Console.Write(arr1[i] + " "); Thread.Sleep(500); } Console.Write("\n从大到小排列为:"); for (int i = 7; i >= 0; i-- )//循环8次 每次循环结束 取出一个最大值 (按道理应该循环7次 但是为了配合输出最后的最小值 让其循环了8次) { for (int j = 0;j < i;j++)//循环i次 每次循环结束 相对大值往后挪一位 { if (arr1[j] > arr1[j + 1])//若改变条件判断成"<",则最后数组值是从大到小 { int k = arr1[j + 1];//中间变量暂存相对小值 arr1[j + 1] = arr1[j]; arr1[j] = k; } } Console.Write(arr1[i] + " "); } Console.Write("\n从小到大排列:"); for (int i = 0; i < 8; i++) Console.Write(arr1[i] + " "); Console.ReadLine();