62,628
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
int[] nums = {9, 0, 3, 2, 4, 1, 5, 8, 7, 6};
for (int i = 0; i < nums.length -1; i++) {
//用maxIndex 保存最大元素的索引
int maxIndex = i;
for (int j = i +1; j < nums.length; j++) {
if(nums[j] > nums[maxIndex])
maxIndex = j;
}
//外循环交换
int temp = nums[i];
nums[i] = nums[maxIndex];
nums[maxIndex] = temp;
}
}