zoukankan      html  css  js  c++  java
  • 算法复习--直接插入排序

    直接插入排序是稳定排序(相同的值不会改变原来的顺序)

    直接插入排序的算法思路:
    (1) 设置监视哨r[0],将待插入纪录的值赋值给r[0];
    (2) 设置开始查找的位置j;
    (3) 在数组中进行搜索,搜索中将第j个纪录后移,直至r[0].key≥r[j].key为止;
    (4) 将r[0]插入r[j+1]的位置上。
    public class InsertSort {    
        public static void main(String[] args){
            int[] a = {50,16,60,17,20,2,5};
            System.out.println("排序前:");
            for(int i = 0; i < a.length; i++){
                System.out.print(a[i] + " ");
            }
            
            //从小到大直接插入排序
            for(int i = 1; i < a.length; i++){
                int temp = a[i]; //比较值
                int j = 0;
                for(j = i - 1; j >= 0; j--){
                    if( a[j] > temp){
                        a[j+1] = a[j];
                    }else{
                        break;
                    }
                }
                a[j+1] = temp;
                System.out.println();
                System.out.println("第" + i + "次排序:");
                for(int n = 0; n < a.length; n++){
                    System.out.print(a[n] + " ");
                }            
            }
        }
    }

  • 相关阅读:
    1025 反转链表
    Vue--修饰符
    Vue--watch
    Vue--防止页面闪烁
    Vue--过滤器
    Vue--自定义指令
    Vue--生命周期
    vue--父子组件传递数据
    vue--父子组件调用彼此的方法
    Celery--beat
  • 原文地址:https://www.cnblogs.com/bincoding/p/5375644.html
Copyright © 2011-2022 走看看