package com.hyl.suanfa;
/**
* 选择排序
*/
public class XuanZeSort {
/*
思路:1、指定下标maxIndex的值为最大,然后每个值依次和下标maxIndex的值比较,谁大就把当前下标替换给maxIndex,找到最大值的下标
2、有了下标,取到把最大值和最后一个值,把最大值换到最后
3、依次循环2、得到排序结果
*/
public static void main(String[] args) {
int[] nums = {4, 2, 3, 5, 1};
for (int j = 0; j < nums.length; j++) {
int maxIndex = 0;
for (int i = 1; i < nums.length - j; i++) {
if (nums[i] > nums[maxIndex]) {
maxIndex = i;//最大值下标
}
}
int num1 = nums[nums.length - j - 1];//最后一位的值
int num2 = nums[maxIndex];//最大值
nums[maxIndex] = num1;
nums[nums.length - j - 1] = num2;//最大的值放到最后
}
for (int num : nums) {
System.out.println(num);
}
}
}