自己的思路:
public class maopaopaixu { public static void main(String[] args) { int [] arr = {24,64,26,89,45}; int tmp = 0; for (int l=0;l<4;l++) { for (int j = 0; j < 4; j++) { if (arr[j] > arr[j + 1]) { tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } // System.out.println("第一轮结束"); for (int j=0;j<arr.length;j++){ System.out.print("第"+j+"个数是"+arr[j]); } } }
视频的思路:
public class maopaopaixu { public static void main(String[] args) { int [] arr = {24,64,26,89,45}; int tmp = 0; for (int l=0;l<4;l++) { for (int j = 0; j < 4-j; j++) { 这个是因为每次需要排序的次数递减,自己的思路是每次的排序次数固定,浪费内存 if (arr[j] > arr[j + 1]) { tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } // System.out.println("第一轮结束"); for (int j=0;j<arr.length;j++){ System.out.print("第"+j+"个数是"+arr[j]); } } }
标签:tmp,arr,java,int,System,冒泡排序,2023.1,public From: https://www.cnblogs.com/canlang9511/p/17028836.html