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

    /**
     * 直接插入排序
     * @author TMAC-J
     * 默认按照从小到大的顺序排列
     * 思路:从所有数中选取一个最小的数,用来和第一个数交换,然后再从剩下的数中选取一个最小的数
     * 用来和第二个数交换,重复此操作
     *
     */
    public class InsertSort {
        
    private int[] array;
        
        public InsertSort(int[] array) {
            this.array = array;
        }
        /**
         * 按从小到大的顺序排列
         */
        public void calculate(){
            boolean isSwitch = false;
            int temp = 0;
            int which = 0;
            for(int i = 0;i<array.length-1;i++){
                for(int j = i;j<array.length-1;j++){
                    if(array[j]>array[j+1]){
                        which = j+1;
                        isSwitch = true;
                    }
                }
                if(isSwitch){
                    temp = array[i];
                    array[i] = array[which];
                    array[which] = temp;
                    isSwitch = false;
                }
            }
        }
        
        public static void main(String[] args) {
            int[] a = {10,9,8,7,6,5,4,3,2,1};
            InsertSort insertSort  = new InsertSort(a);
            insertSort.calculate();
            for(int i = 0;i<a.length;i++){
                System.out.println(a[i]);
            }
        }
        
    }
  • 相关阅读:
    大三寒假生活19
    大三寒假生活18
    大三寒假生活17
    大三寒假生活16
    大三寒假生活15
    大三寒假生活14
    MySQL 字符集与比较规则
    Python ord & chr
    CentOS7 通过 devstack 安装 OpenStack
    Python *args & **kwargs
  • 原文地址:https://www.cnblogs.com/yzjT-mac/p/6253744.html
Copyright © 2011-2022 走看看