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

    选择排序思想

      第一次从下标为0的数字开始与后面的n-1个进行比较;找出最小或者最大的放在下标为0的这个位置;第二次从下标为1的开始比较;查询剩下的最大或者最小值;放在 
    下标为1的位置;以此类推;直到排序完成

    具体代码示例

    package com.chenpt.arithmetic;
    
    import java.util.Arrays;
    
    /**
     * @Author: chen
     * @Description:  选择排序
     * @Date: created in 2018/9/7
     * @Modified By:
     */
    public class SelectSort {
    
        public static void operation(){
            int[] arr = {9,3,5,8,2};
            System.out.println("排序前:");
            Arrays.stream(arr).forEach(x->{
                System.out.print(x+" ");
            });
    
            for (int i=0;i<arr.length-1;i++){
                for (int j=i+1;j<arr.length;j++){
                    if(arr[i]>arr[j]){
                        int x = arr[i];
                        arr[i] = arr[j];
                        arr[j] = x;
                    }
                }
            }
    
            System.out.println("
    排序后:");
            for (int num : arr){
                System.out.print(num+" ");
            }
    
        }
    
        public static void main(String[] ar){
            operation();
        }
    
    }
    //执行结果
    排序前:
    9 3 5 8 2 
    排序后:
    2 3 5 8 9 
    

      

  • 相关阅读:
    多线程 -- H2O 生成、交替打印字符串
    打印零与奇偶数
    h2数据库的使用
    rtx应用和开发
    MongoDB--副本集
    Python 推导式
    Bootstrap组件
    Python logging日志的自动分割
    python watchdog监控文件修改
    Linux流量监控iftop
  • 原文地址:https://www.cnblogs.com/chenpt/p/9603363.html
Copyright © 2011-2022 走看看