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 是不稳定的排序。

  • 相关阅读:
    快速排序
    ABP Error in roboto.css can't resolve '97uahxiqZRoncBaCEI3aWxJtnKITppOI_IvcXXDNrsc.woff2'
    .NET Core Log
    .NET Core的配置文件
    VirtualBox多网卡模式
    Maven 常见错误
    python压缩文件脚本
    Windows7 64bit 安装python3.3 & cx_Freeze-4.3.2
    Ubuntu Linux环境变量
    Ubuntu12.04 64bit 安装 Dropbox
  • 原文地址:https://www.cnblogs.com/james1207/p/3277983.html
Copyright © 2011-2022 走看看