冒泡排序有两种方法:一种是从大到小,一种是从小到大
如图:
举例:
int[] nums = new int[] { 98, 54, 54, 322, 43, 23 }; int temp = 0; for (int j = 0; j < nums.Length-1; j++)//j是比较的轮数 { for (int i = 0; i < nums.Length - 1 - j; i++) { if (nums[i] > nums[i + 1]) { temp = nums[i]; nums[i] = nums[i + 1]; nums[i + 1] = temp; } } } for (int i = 0; i < nums.Length; i++) { Console.Write(nums[i]+"\t"); } Console.ReadKey();