![]()
package com.programme.demo01;
import java.util.Arrays;
/**
* @program: springboottest
* @description: ${description}
* @author: Mr.zw
* @create: 2021-05-15 20:48
**/
public class Demo02 {
public static void main(String[] args) {
int[] arr = {5,3,2,4,1};
for (int i = 0; i < arr.length-1; i++) {
//-1 是为了防止索引越界i
for (int j = 0; j < arr.length-1-i; j++) {
//System.out.println(j+"--"+(j+1));
//j和j+1比较
//如果j索引元素,大于j+1索引元素,就交换
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}