最近准备换工作,然后面了几家公司,收到了一份还算满意的offer。面试过程中发现大公司都会喜欢面一些算法题。对于经常关注算法的人而言可能并不难,因为都是一些基础题,比如快速排序、冒泡排序、二叉树等等。但是对于平时工作从不关心算法的人而言那可真的够头大的。今天复习了冒泡排序,顺便记录下。也提醒下自己多多关注算法题,没事刷一刷总不是坏事。
package com.lixiang.algorithm;
/**
* 冒泡排序
*
* @author lixiang
* @date 2019年08月08日 - 22:23
* @history 2019年08月08日 - 22:23 lixiang create.
*/
public class Maopaopaixu {
public static void paixu(int[] args) {
for (int i = 0; i < args.length - 1; i++) {
for (int j = 0; j < args.length - i - 1; j++) {
if (args[j] > args[j + 1]) {
int a = args[j];
args[j] = args[j + 1];
args[j + 1] = a;
}
}
}
for (int i : args) {
System.out.print(i + " ");
}
}
public static void main(String[] args) {
int[] array = {19, 31, 22, 6, 18, 7, 1, 12, 3, 8, 5, 21, 15};
paixu(array);
}
}
测试结果: