JAVA冒泡排序学习
视频https://www.bilibili.com/video/BV1E8411e718
package com.horse;
import java.util.Arrays;
public class Sort {
public static void main(String[] args) {
int arr[] = {4, 1, 8, 2};
System.out.println(Arrays.toString(arr));//打印排序前数组
bubbleSort(arr);
System.out.println(Arrays.toString(arr));//打印排序后数组
}
public static void bubbleSort(int arr[]) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {//比较大小,对换位置操作
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}