集册 Java实例教程 选择排序整数数组

选择排序整数数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

466
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
选择排序整数数组

public class Main {// 来自 时 代 J a v a - N o w J a v a . c o m

    static int nboperations;


    static boolean GreaterThan(int a, int b) {

        nboperations++;

        return (a > b);

    }


    static void swap(int[] array, int i, int j) {

        nboperations++;

        int tmp = array[i];

        array[i] = array[j];

        array[j] = tmp;

    }


    static void SelectionSort(int[] array) {// 来 自 n o w j a v a . c o m - 时代Java

        int n = array.length;


        for (int i = 0; i < n - 1; i++) {

            for (int j = i + 1; j < n; j++) {

                if (GreaterThan(array[i], array[j]))

                    swap(array, i, j);

            }

        }

    }


    public static void main(String[] args) {

        int[] array = {16, 15, 114, 13, 12, 11, 1110, 91, 8, 7, 6, 51, 4, 3, 2, 1};

        nboperations = 0;


        SelectionSort(array);


        for (int i = 0; i < array.length; i++)

            System.out.print(array[i] + " ");

        System.out.prin
展开阅读全文