编程语言
首页 > 编程语言> > java selection sort

java selection sort

作者:互联网

java selectionSort

public class SelectionSort {
    public static void main(String[] args) {
        int[] arr = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void selectionSort(int[] arr) {
        for (int j = 0; j < arr.length; j++) {
            int minIndex = j;
            for (int i = j + 1; i < arr.length; i++) {
                if (arr[i] < arr[minIndex]) {
                    minIndex = i;
                }
            }
            int temp = arr[j];
            arr[j] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

}

标签:sort,minIndex,arr,selection,java,selectionSort,int,static,public
来源: https://www.cnblogs.com/wilwei/p/11329561.html