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

    算法描述:对于给定的一组记录,经过第一轮比较后得到最小的记录,然后将该记录与第一个记录的位置进行交换;
    接着对不包括第一个记录以外的其他记录进行第二轮比较,得到最小的记录并与第二个记录进行位置交换;重复该过程,直到进行比较的记录只有一个时为止。

    package sorting;
     
    /**
     * 选择排序
     * 平均O(n^2),最好O(n^2),最坏O(n^2);空间复杂度O(1);不稳定;简单
     * @author zeng
     *
     */
    public class SelectionSort {
     
        public static void selectionSort(int[] a) {
            int n = a.length;
            for (int i = 0; i < n; i++) {
                int k = i;
                // 找出最小值的小标
                for (int j = i + 1; j < n; j++) {
                    if (a[j] < a[k]) {
                        k = j;
                    }
                }
                // 将最小值放到排序序列末尾
                if (k > i) {
                    int tmp = a[i];
                    a[i] = a[k];
                    a[k] = tmp;
                }
            }
        }
     
        public static void main(String[] args) {
            int[] b = { 4938659776132750 };
            selectionSort(b);
            for (int i : b)
                System.out.print(i + " ");
        }
    }
    来自:https://www.cnblogs.com/zengzhihua/p/4456741.html  仅供学习

  • 相关阅读:
    shell常用的系统变量
    Git的使用--如何将本地项目上传到Github
    vmware + centos 7安装vmtools时提示The path "" is not a valid path to the xxx kernel header
    SQL查询表中的有那些索引
    SQL merge into 表合并
    SqlServer coalesce函数
    SQL 大数据查询如何进行优化?
    为什么GOF的23种设计模式里面没有MVC?
    Javascript闭包
    AngularJS概述&指令
  • 原文地址:https://www.cnblogs.com/altlb/p/8318060.html
Copyright © 2011-2022 走看看