选择排序

选择排序算法:

  思想: 每次在未排序得数组中找到最小的放到排序数组末尾

  时间复杂度:  O(n²) 


(一) 代码

public class SelectionSort {

    public static void main(String[] args) {
        int arr[] = new int[]{3,3,4,7,13,435,54,2,6666,234};
        selectSort(arr);
        System.out.print(Arrays.toString(arr));
    }

    private static void selectSort(int[] arr) {
        for(int i = 0 ; i < arr.length-1 ; i++){
            //最小值下标
            int min = i;
            for(int j = i+1; j < arr.length ; j++){
                if(arr[j] < arr[i]){
                    min = j;
                }
            }
            if(i != min){
                int temp = arr[min];
                arr[min] = arr[i];
                arr[i] = temp;
            }
        }
    }
}

      人生就是在不断的选择

    

原文地址:https://www.cnblogs.com/misscai/p/14954679.html