static void Main(string[] args) { int[] array = { 23, 45, 16, 7, 42 }; int length = array.Length - 1; bool isExchanged = false; for (int i = 0; i < length; i++) { isExchanged = false; for (int j = length; j > i; j--) { if (array[j] > array[j - 1]) { int temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; isExchanged = true; } } if (!isExchanged) //一遍比较过后如果没有进行交换则退出循环 break; } foreach (int i in array) { Console.WriteLine(i); } Console.Read(); }
冒泡排序
最新推荐文章于 2022-04-29 23:24:15 发布