zoukankan      html  css  js  c++  java
  • 选择排序

    基础排序参考
    https://blog.csdn.net/yushiyi6453/article/details/76407640

    选择排序

    排序从小到大排序:一开始从0~n-1 区间上选择一个最小值,将其放在位置0上,然后在1~n-1范围上选取最小值放在位置1上。重复过程直到剩下最后一个元素,数组即为有序。

    package selectsort;

    import java.util.Scanner;

    /**
     * @author WangXiaoeZhe
     * @Date: Created in 2019/11/21 16:29
     * @description:
     */

    public class SelectSort {
        /**
         * 选择排序
         *
         * @param args
         */

        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int[] arr = new int[5];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = scanner.nextInt();
            }
            int[] ints = selectSort(arr);
            for (int i = 0; i < ints.length; i++) {
                System.out.println(arr[i]);
            }
        }

        private static int[] selectSort(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                int minIndex = i;
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[minIndex] > arr[j]) {
                        minIndex = j;
                    }
                }
                if (minIndex != i) {
                    swap(arr,i,minIndex);
                }
            }

            return arr;
        }

        public static void swap(int[] arr, int i, int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }

    }

    平均时间复杂度: O(n^2) 最坏时间复杂度:O(n^2) 空间复杂度: O(1) 不稳定

  • 相关阅读:
    apache与tomcat负载集群的3种方法
    JFinal 源码分析 [DB+ActiveRecord]
    通过PowerShell获取TCP响应(类Telnet)
    Linux 硬盘分区、分区、删除分区、格式化、挂载、卸载
    常用EXE文件反编译工具
    Shell采集系统cpu 内存 磁盘 网络信息
    MyEclipse运行很慢的原因
    Java令牌生成器
    Shell 编程基础之基本语法结构汇总
    Shell 编程基础之注意技巧
  • 原文地址:https://www.cnblogs.com/wuhen8866/p/11907376.html
Copyright © 2011-2022 走看看