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

    选择排序

    选择排序特点:每一次从待排序的数据元素中选出最小的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。时间复杂度为

    选择排序代码

    public static void selectionSort(int[] array, int len){
        int minValueIndex;
        for (int i = 0; i < len-1; i++){
            minValueIndex = i; //traverse from i find the index of min value
            for (int j = i+1; j < len; j++){
                if (array[minValueIndex] > array[j]){
                    minValueIndex = j;
                }
            }

            if (minValueIndex != i){ //swap
                int temp = array[minValueIndex];
                array[minValueIndex] = array[i];
                array[i] = temp;
            }
        }
    }

    测试样例

    • 2 1 3 5 4
    第1次遍历之后数组为:1 2 3 5 4 
    第2次遍历之后数组为:1 2 3 5 4 
    第3次遍历之后数组为:1 2 3 5 4 
    第4次遍历之后数组为:1 2 3 4 5 

    ----- end -----

     

  • 相关阅读:
    docker nginx无法连通php
    docker查看ip
    使用docker-compose部署nginx
    docker启动服务---------------nginx+php
    docker下安装kafka和kafka-manager
    docker启动服务---------------kafka+zookeeper
    docker将镜像推送到阿里云
    Docker常用命令
    docker-compose编写示例
    docker启动服务
  • 原文地址:https://www.cnblogs.com/denluoyia/p/9673237.html
Copyright © 2011-2022 走看看