package com;
public class MaoPao {
//冒泡排序
public static void main(String[] args) {
int[] arr = {3,2,5,1,8,1,11,8};
int[] result = pai(arr);
for (int a:result) {
System.out.print(a+" ");
}
}
private static int[] pai(int[] arr) {
//升序
//外层for负责循环的次数
for(int i = 0;i<arr.length;i++){
int temp = 0;
//内层for循环控制相邻的两个元素进行比较
for(int j = i+1;j<arr.length;j++){
//若是升序判断条件为arr[i]>arr[j]
//若是降序判断条件为arr[i]<arr[j]
if (arr[i]>arr[j]){
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}
}
冒泡排序
最新推荐文章于 2022-04-29 23:24:15 发布