JavaSE中数组的冒泡排序:
冒泡排序的特点是,每一轮循环后,最大的一个数被交换到末尾,因此,下一轮循环就可以“刨除”最后的数,每一轮循环都比上一轮循环的结束位置靠前一位。
以 int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 }为例。
程序:
public static void main(String[] args) {
int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
//排序前
System.out.println("排序前:"+Arrays.toString(ns));
//冒泡排序
for (int i = 0; i < ns.length-1; i++) {
for (int j = 0; j < ns.length-i-1; j++) {
if(ns[j]>ns[j+1]) {
//交换(ns[j]和ns[j+1]
int tmp = ns[j];
ns[j] = ns[j+1];
ns[j+1] = tmp;
}
}
System.out.println("当i变化时:"+Arrays.toString(ns));
}
//排序后
System.out.println("排序后:"+Arrays.toString(ns));
}
原文:https://www.cnblogs.com/Ronin-Ma/p/14617061.html