Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

常用 Java 代码块 #31

Open Qingquan-Li opened 7 years ago

Qingquan-Li commented 7 years ago
//HelloWorld
public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello World!");
    }
}
//以“年-月-日”格式获取当前时间
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDate = sdf.format(date);
/**
冒泡排序算法的运作如下(从后往前):
1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
3. 针对所有的元素重复以上的步骤,除了最后一个。
4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
*/
public class BubbleSort{
    public void sort(int[] a){
        int temp = 0;
        for (int i = a.length - 1; i > 0; --i){
            for (int j = 0; j < i; ++j){
                if (a[j + 1] < a[j]){
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
}