Java 冒泡排序
public static void main(String[] args) {
int[] a = {5,4,2,1,3,6,8,9,10};
for (int i = 0; i < a.length - 1; i++) {
for (int ii = 0;ii < a.length - 1 - i;ii++){
if (a[ii] > a[ii+1]){
a[ii] = a[ii] ^ a[ii+1];
a[ii+1] = a[ii] ^ a[ii+1];
a[ii] = a[ii] ^ a[ii+1];
}
}
}
System.out.println(Arrays.toString(a));
}
}
结果
[1, 2, 3, 4, 5, 6, 8, 9, 10]
评论 (0)