直接上代码:
大到小的我加了输出语句,方便想要看走到哪一步每一位的数是多少的童鞋,原理就是先定位置,逐一比较,找到最小/最大的,换下一个数再进行比较。
package test;
/**
*
* @author 作者Bowen
* @data 创建时间:2019年7月2日 下午13:07:35
* @ClassName testA
* @Description 冒泡排序
*
*/
public class testA {
public static void main(String[] args) {
int a[] = { 3, 2, 1, 6, 5, 4 };
small(a);
// big(a);
System.out.println("*** 打印 ***");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
public static void small(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int b = a[i];
a[i] = a[j];
a[j] = b;
}
}
}
}
public static void big(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println(i + " !!!!!!!!!! ");