Java 选择排序
- 从0索引开始,拿着每一个索引上的元素跟后面的元素依次比较
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 j = i + 1; j < a.length; j++) {
if (a[i] > a[j]){
a[i] = a[i] ^ a[j];
a[j] = a[i] ^ a[j];
a[i] = a[i] ^ a[j];
}
}
}
System.out.println(Arrays.toString(a));
}
结果
[1, 2, 3, 4, 5, 6, 8, 9, 10]
评论 (0)