package com.atguigu.sort;
import java.util.Arrays;
/**
* 选择排序
*
* @author Dxkstart
* @create 2021-10-11-16:34
*/
public class SelectSort {
public static void main(String[] args) {
int[] arr = {101, 34, 119, 1,-1,90,12};
selectSort(arr);
}
//选择排序
//时间复杂度为:O(n^2)
public static void selectSort(int[] arr) {
int minIndex;//最小值的数组索引值
int min;//最小值
for (int i = 0; i < arr.length - 1; i++) {
minIndex = i;
min = arr[minIndex];
for (int j = i + 1; j < arr.length; j++) {
if (min > arr[j]) {
min = arr[j];
minIndex = j;
}
}
//交换
if (minIndex != i) {
arr[minIndex] = arr[i];
arr[i] = min;
}
System.out.printf("第%d趟排序结果:\n",i + 1);
System.out.println(Arrays.toString(arr));
}
}
}