zoukankan      html  css  js  c++  java
  • 8种排序算法--直接选择排序

    直接选择排:这种排序思想。直接选择排序顾名思义:就是重一堆中每次取出最大数(或者最小数)依次排列:

    有这么一组数:9 1 5 4 8 7 4 6 6

    采用选择排序是这么的:

    第1次                  1 9 5 4 8 7 4 6 6   首先重开始的里面选择最小的,交换位置

    第2次                  1 4 5 9 8 7 4 6 6    接着又从上面黑色数字里面选最小的

    第3次                  1 4 4 9 8 7 5 6 6    重复

    第4次                  1 4 4 58 7 9 6 6

    第5次                  1 4 4 56 7 9 8 6

    第6次                  1 4 4 5 6 6 9 8 7

    第7次                  1 4 4 56 6 7 8 9

    第8次                   1 4 4 5 6 6 7 8 9

     

     

    下面看看代码实现:

    packagecom.fish.sort;

     

    public class SelectSort {

        public static void main(String[] args) {

            // 将九个数进行排序

            int[] array = { 9, 1, 5, 4, 8, 7, 4, 6, 6 };

     

            // 排序

            myResult(array);

     

        }

     

        public static void myResult(int[] array) {

            int minIndex, swap;//用一个minindex记住最小的值的索引

     

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

                minIndex = i;

     

                for (int j = i + 1; j < array.length; j++) {

                    if (array[minIndex] > array[j]) {

                        minIndex = j;//获取最小值索引的方法

     

                    }

     

                }

     

                if (i != minIndex) {//如果索引变化了说明原先不是最小值相互想换位置

                    swap = array[i];

                    array[i] = array[minIndex];

                    array[minIndex] = swap;

     

                }

     

            }

            System.out.println("结果是:");

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

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

            }

        }

    }

    这种排序时间算法复杂度是n^2 空间复杂度为1 是不稳定的排序。

  • 相关阅读:
    laravel报错1071 Specified key was too long; max key length is 1000 bytes
    【laravel】Eloquent 模型事件和监听方式
    angular使用forRoot() 注册单一实例服务
    js判断电脑是windows系统还是mac系统
    扁平数据根据`parentId`生成树结构
    页面滚动到指定元素区域
    js简洁模式代码
    简单git使用命令
    图片懒加载 echo.js
    页面图片预加载与懒加载
  • 原文地址:https://www.cnblogs.com/james1207/p/3277983.html
Copyright © 2011-2022 走看看