import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {33,14,44,23,63,27};
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void bubbleSort(int[] arr){
for (int i = 0; i < arr.length-1; i++) {
// 因为完成了i个,所以减i
for (int j = 0; j < arr.length-1-i; j++) {
if(arr[j]>arr[j+1]){
int temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
结果:[14, 23, 27, 33, 44, 63]
来源:CSDN
作者:聋
链接:https://blog.csdn.net/weixin_42313246/article/details/104576204