zoukankan      html  css  js  c++  java
  • 排序算法(二)两种选择排序

    SortUtil.java

    package com.zby.sort;
    
    import java.util.Arrays;
    import java.util.Random;
    import java.util.function.Consumer;
    
    /**
     * @author zby
     * @title SortUtil
     * @date 2019年7月15日
     * @description 排序工具类
     */
    public class SortUtil {
    
        // 打印排序数组的阀值
        public static int PRINT_THRESHOLD = 100;
    
        /**
         * 
         * @param size
         * @return 获取一个随机数组
         */
        public static int[] getIntArray(int size) {
            System.out.println("数组大小:" + size);
            // 初始化待排序数组
            int[] arrOrig = new int[size];
            for (int i = 0; i < size; i++) {
                Random random = new Random();
                arrOrig[i] = random.nextInt(size + 100);
            }
            if (arrOrig.length <= PRINT_THRESHOLD) {
                System.out.println("排序前数组:" + Arrays.toString(arrOrig));
            }
            return arrOrig;
        }
    
        /**
         * 封装排序算法运行
         * 
         * @param name     算法名称
         * @param arrOrig  待排序数组
         * @param consumer 排序算法
         */
        public static void run(String name, int[] arrOrig, Consumer<int[]> consumer) {
            System.out.println("********************");
            int[] arr = new int[arrOrig.length];
            System.arraycopy(arrOrig, 0, arr, 0, arrOrig.length);
            Long start = System.currentTimeMillis();
            consumer.accept(arr);
            System.out.printf("%s 耗时 :%d ms 
    ", name, System.currentTimeMillis() - start);
            if (arrOrig.length <= PRINT_THRESHOLD) {
                System.out.println("排序后:" + Arrays.toString(arr));
            }
        }
    }

    SelectSort.java

    package com.zby.sort;
    
    /**
     * @author zby
     * @title BubbleSort
     * @date 2019年7月9日
     * @description 冒泡排序
     */
    public class SelectSort {
    
        public static void main(String[] args) {
            run(10);
            run(10000);
            run(100000);
        }
    
        private static void run(int size) {
            int[] intArrayShiwan = SortUtil.getIntArray(size);
            SortUtil.run("选择排序(直接交换)", intArrayShiwan, SelectSort::selectSortWithTempExchange);
            SortUtil.run("选择排序(索引交换)", intArrayShiwan, SelectSort::selectSortExchangeOptimize);
        }
    
        /**
         * 选择排序
         * 
         * @param arr 待排序数组
         */
        public static void selectSortWithTempExchange(int[] arr) {
            int temp;
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] > arr[j]) {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
        }
    
        /**
         * 选择排序(交换优化)
         * 
         * @param arr 待排序数组
         */
        public static void selectSortExchangeOptimize(int[] arr) {
            int temp;
            int maxIndex;
            for (int i = 0; i < arr.length - 1; i++) {
                maxIndex = i;
                for (int j = i + 1; j <= arr.length - 1; j++) {
                    if (arr[maxIndex] > arr[j]) {
                        maxIndex = j;
                    }
                }
                if (i != maxIndex) {
                    temp = arr[maxIndex];
                    arr[maxIndex] = arr[i];
                    arr[i] = temp;
                }
            }
        }
    
    }

    Console

    数组大小:10
    排序前数组:[11, 95, 38, 15, 52, 104, 31, 81, 6, 28]
    ********************
    选择排序(直接交换) 耗时 :0 ms 
    排序后:[6, 11, 15, 28, 31, 38, 52, 81, 95, 104]
    ********************
    选择排序(索引交换) 耗时 :0 ms 
    排序后:[6, 11, 15, 28, 31, 38, 52, 81, 95, 104]
    数组大小:10000
    ********************
    选择排序(直接交换) 耗时 :145 ms 
    ********************
    选择排序(索引交换) 耗时 :31 ms 
    数组大小:100000
    ********************
    选择排序(直接交换) 耗时 :14131 ms 
    ********************
    选择排序(索引交换) 耗时 :2392 ms 
  • 相关阅读:
    畅通工程续 (dijkstra)
    最短路径问题 HDU 3790
    【基础算法-模拟-例题-玩具谜题】-C++
    【基础算法-模拟-例题-金币】-C++
    【动态规划例题-数塔问题】-C++
    【基本数据结构之'图'】
    【最小生成树之Kruskal例题-建设电力系统】-C++
    【最短路算法例题-升降梯上】-C++
    【基本数据结构之栈】
    【栈-例题】网页跳转-C++
  • 原文地址:https://www.cnblogs.com/zby9527/p/11236460.html
Copyright © 2011-2022 走看看