java排序之选择排序

package com.test.sort;

public class SelectSort {

    public static void sort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = array.length -1; j > i; j--) {
                if (array[j] < array[i]) {
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

    public static void printArray(int[] array) {
        System.out.print("{");
        for (int i = 0; i < array.length; i ++) {
            System.out.print(array[i]);
            if (i != array.length - 1) {
                System.out.print(",");
            }
        }
        System.out.print("}");
    }

    public static void main(String[] args) {
        int[] array = {3, 5, 2, 7, 3};
        sort(array);
        printArray(array);
    }
}


控制台输出:


{2,3,3,5,7}
Process finished with exit code 0
 

猜你喜欢

转载自blog.csdn.net/qq_34561892/article/details/81836806