for循环实现:
for (int i = 0; i<arr.Length - 1; i++) { for (int j = 0; j<arr.Length - i - 1; j++) { if (arr[j] < arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } }
While循环实现:
int i = 0; while (i <= arr.Length + 1) { int j = 0; while (j < arr.Length - i - 1) { if (arr[j] < arr[j + 1]) //如果左边比右边小,左右换位置 { T temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } j++; } i++; }