题解 | #冒泡排序#
冒泡排序
https://www.nowcoder.com/practice/83ef53227d654df28c18fd6a377e8fee
import java.util.Arrays; import java.util.Scanner; public class Main { // 交换 public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void bubbleSort(int[] arr) { // 不需要排序的情况 if (arr == null || arr.length < 2) return; // N - 1 趟 for (int end = arr.length - 1; end > 0; end--) { for (int i = 0; i < end; i++) { // 比较大小 if (arr[i] > arr[i + 1]) swap(arr, i, i + 1); } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] arr = new int[7]; for (int i = 0; i < arr.length; i++) { arr[i] = scanner.nextInt(); } scanner.close(); //write your code here...... bubbleSort(arr); for (int k = 0; k < arr.length; k++) { System.out.print(arr[k] + " "); } } }#冒泡排序divdivforiinrangen1divdivforjinrangeni1divdivifbjgtbj1divdivbjbj1bj1bjdivdivajaj1aj1ajdivdiv#