选择排序

public class SelectionSort {
    public static void main(String[] args) {

        int[] array = {1,3,4,8,7,4,6,9,0};
        for (int i : Selection(array)) {
            System.out.println(i);
        }
    }

     public static int[] Selection(int[] array){
         for (int i = 0; i < array.length - 1; i++) {
             int minIndex = i;
             for (int j = i +1; j < array.length; j++) {
                 if (array[minIndex] > array[j]){
                     minIndex = j;
                 }
             }
             swap(array, i, minIndex);
         }
         return array;
     }

     public static void swap(int[] array, int i, int j){
        if (i == j){
            return;
        }
        array[i] = array[i] ^ array[j];
        array[j] = array[i] ^ array[j];
        array[i] = array[i] ^ array[j];
     }
}
上一篇:算法学习之路(排序)


下一篇:排序算法1 - 选择排序