长时间没有关注这些底层的方法,万一面试的时候卡壳了岂不是很尴尬,所以抽空复习一下。
public class DiGui {
public static void main(String[] args) {
System.out.println(diGui(5));
bubbleSort();
}
public static int diGui(int n){
if(n<=1){
return 1;
}else{
return n*diGui(n-1);
}
}
public static void bubbleSort(){
int a[] = {5,4,3,2,1};
for(int j = 0 ; j < a.length - 1; j++){ // a.length - 1 标示 n - 1轮
for (int i = 0; i < a.length - 1 - j; i++){//这里减去一个 " j" ,那么每次比较 “对应的倒数位置”的数字就不会对比了
if(a[i] > a[1+i]){
int temp = a[i+1];
a[i+1] = a[i];
a[i] = temp;
}
}
}
for(int b:a){
System.out.println(b);
}
}
}