学习记录分享(冒泡排序)
public class Test {
public static void main(String[] args) {
int[] array = {45,1,2,1,5,45,11,23,8,7,8,9,3};
sort(array);
System.out.println(Arrays.toString(array));
}
public static void sort(int[] array){
//临时变量
int temp = 0;
//外层循环,判断我们这个要走多少次;
for (int i = 0; i < array.length-1; i++) {
//内层循环,比较判断两个数,如果第一个数比第二个数大,则交换位置(升序)
//降序将array[j]>array[j+1]改为array[j]<array[j+1]即可
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;
}
}
}
}
}
//执行结果:[1, 1, 2, 3, 5, 7, 8, 8, 9, 11, 23, 45, 45]