zoukankan      html  css  js  c++  java
  • 插入排序实现

     1 public class InsertSort {
    2
    3 /**
    4 * @param args
    5 */
    6 public static void main(String[] args) {
    7 // TODO Auto-generated method stub
    8 int []ary =new int[]{2,5,1,4,8,3,6,9};
    9 //int []ary={2,5,1,4,8,3,6,9};
    10 ary=insertSort(ary);
    11 for(int i=0;i<ary.length;i++)
    12 {
    13 System.out.print(ary[i]+" ");
    14 }
    15 }
    16
    17 public static int [] insertSort(int []ary)
    18 {
    19 for(int i=1;i<ary.length;i++)
    20 {
    21 int temp=ary[i];
    22 int j;
    23 //for(j=i-1;j>=0&&temp<ary[j];j--)
    24 for(j=i-1;j>=0;j--)
    25 {
    26 if(temp<ary[j])
    27 {
    28 ary[j+1]=ary[j];
    29 }else
    30 break;
    31 }
    32 ary[j+1]=temp;//插入
    33 }
    34 return ary;
    35 }
    36 }

  • 相关阅读:
    day23
    day22
    day21
    day20
    小程序 组件操作
    jmeter安装使用一
    小程序登录操作
    Django ORM DateTimeField 时间误差8小时问题
    小程序初始篇
    ADB命令
  • 原文地址:https://www.cnblogs.com/superjt/p/2117637.html
Copyright © 2011-2022 走看看