//冒泡排序:
//1.较数组中两个,两个相邻的元素,若第一个数比第二个数大,则交换值。
//2.每一轮比较,都会产生一个最大或最小的数字。
//3.下一轮则少一次排序。、
//4.依次循环,直至结束。
public class test01 {
public static void main(String[] args) {
int[] a = {1,85,95,5,33,45,78,456,4};
int[] sort = sort(a);
System.out.println(Arrays.toString(sort));
}
public static int[] sort(int[] array) {
//临时变量,用于比较后交换值。
int temp = 0;
//外层循环,判断要走多少次。
for(int i = 0; i <array.length-1; i++) {
//内层循环,比较判断两个数,比第二个数大,则交换位置。
for(int j = 0; j<array.length-1-i ; j++) {
if(array[j]>array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
}
输出结果:[1, 4, 5, 33, 45, 78, 85, 95, 456]
原文:https://www.cnblogs.com/xiaokai55/p/13769328.html