1. package com.atguigu.sort;
    2. import java.util.Arrays;
    3. /**
    4. * 选择排序
    5. *
    6. * @author Dxkstart
    7. * @create 2021-10-11-16:34
    8. */
    9. public class SelectSort {
    10. public static void main(String[] args) {
    11. int[] arr = {101, 34, 119, 1,-1,90,12};
    12. selectSort(arr);
    13. }
    14. //选择排序
    15. //时间复杂度为:O(n^2)
    16. public static void selectSort(int[] arr) {
    17. int minIndex;//最小值的数组索引值
    18. int min;//最小值
    19. for (int i = 0; i < arr.length - 1; i++) {
    20. minIndex = i;
    21. min = arr[minIndex];
    22. for (int j = i + 1; j < arr.length; j++) {
    23. if (min > arr[j]) {
    24. min = arr[j];
    25. minIndex = j;
    26. }
    27. }
    28. //交换
    29. if (minIndex != i) {
    30. arr[minIndex] = arr[i];
    31. arr[i] = min;
    32. }
    33. System.out.printf("第%d趟排序结果:\n",i + 1);
    34. System.out.println(Arrays.toString(arr));
    35. }
    36. }
    37. }