原理:比较相邻的两个元素,将值大的元素交换到右边(降序则相反).
int[] ints = {20, 33, 42, 41, 21, 123, 7, 954, 7, 99, 86, 5, 8, 76, 5434, 75, 7563, 235, 864, 84, 32678,3,1};
int length = ints.length;
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < length - 1 - i; j++) {
if (ints[j] > ints[j + 1]) {
int temp = ints[j + 1];
ints[j + 1] = ints[j];
ints[j] = temp;
}
}
}
本文参考:
https://www.cnblogs.com/onepixel/articles/7674659.html