• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

无信不立

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

【数据结构和算法】插入排序

一、插入排序java代码

package com.spring.test.service.algorithm.sort;

/**
 * 插入排序算法
 * @date 7:54 PM 2019/5/13
 */
public class InsertionSort {

    public static void main(String[] args) {
        test01();
    }

    public static void test01(){
        int[] arrays={5,6,8,0,9,3,2,1,4,7,10};
        sort(arrays);
        for(int a:arrays){
            System.out.println(a);
        }
    }

    public static void sort(int[] arrays){
        for(int i=1;i<arrays.length;i++){
            int a=arrays[i];
            int j=i-1;
            while (j>=0&&arrays[j]>a){
                arrays[j+1]=arrays[j];
                j=j-1;
            }
            arrays[j+1]=a;
        }
    }
}
View Code

 

二、循环不变式理解算法的正确性

 

posted on 2019-05-19 19:09  无信不立  阅读(92)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3