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

    方法一:

    package com.wenzhi.util;
    
    import java.util.Arrays;
    
    public class Demo01 {
    
    	/**
    	 * 插入排序
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		int ary[]=new int[]{ 69,60,78,90,76,85,98,64,66,82};
    		for(int i=0;i<ary.length;i++){	
    			i=ary[i];
    		}
    		System.out.println(Arrays.toString(ary));
    		System.out.println("插入排序");
    		for(int i=1;i<ary.length;i++){
    			int k=ary[i];
    			int j;
    			for(j= i-1;j>=0&&k<ary[j];j--){
    				ary[j+1]=ary[j];
    			}
    			ary[j+1]=k;
    			System.out.println(Arrays.toString(ary));
    		}
    		System.out.println("排序结束");
    		System.out.println(Arrays.toString(ary));
    	}
    
    }
    


    输出结果

    [69, 60, 78, 90, 76, 85, 98, 64, 66, 82]
    插入排序
    [60, 69, 78, 90, 76, 85, 98, 64, 66, 82]
    [60, 69, 78, 90, 76, 85, 98, 64, 66, 82]
    [60, 69, 78, 90, 76, 85, 98, 64, 66, 82]
    [60, 69, 76, 78, 90, 85, 98, 64, 66, 82]
    [60, 69, 76, 78, 85, 90, 98, 64, 66, 82]
    [60, 69, 76, 78, 85, 90, 98, 64, 66, 82]
    [60, 64, 69, 76, 78, 85, 90, 98, 66, 82]
    [60, 64, 66, 69, 76, 78, 85, 90, 98, 82]
    [60, 64, 66, 69, 76, 78, 82, 85, 90, 98]
    排序结束
    [60, 64, 66, 69, 76, 78, 82, 85, 90, 98]
    


    方法二:

    package com.wenzhi.util;
    
    import java.util.Arrays;
    import java.util.Random;
    
    public class Demo01 {
    
    	/**
    	 * 插入排序
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		int ary[]=new int[12];
    		Random random=new Random();
    		for(int i=0;i<ary.length;i++){
    			ary[i]=random.nextInt(100);
    		}
    		System.out.println(Arrays.toString(ary));
    		System.out.println("插入排序");
    		for(int i=1;i<ary.length;i++){
    			int k=ary[i];
    			int j;
    			for(j= i-1;j>=0&&k<ary[j];j--){
    				ary[j+1]=ary[j];
    			}
    			ary[j+1]=k;
    			System.out.println(Arrays.toString(ary));
    		}
    		System.out.println("排序结束");
    		System.out.println(Arrays.toString(ary));
    	}
    
    }
    


    输出结果

    [54, 14, 75, 98, 67, 51, 98, 97, 54, 61, 27, 55]
    插入排序
    [14, 54, 75, 98, 67, 51, 98, 97, 54, 61, 27, 55]
    [14, 54, 75, 98, 67, 51, 98, 97, 54, 61, 27, 55]
    [14, 54, 75, 98, 67, 51, 98, 97, 54, 61, 27, 55]
    [14, 54, 67, 75, 98, 51, 98, 97, 54, 61, 27, 55]
    [14, 51, 54, 67, 75, 98, 98, 97, 54, 61, 27, 55]
    [14, 51, 54, 67, 75, 98, 98, 97, 54, 61, 27, 55]
    [14, 51, 54, 67, 75, 97, 98, 98, 54, 61, 27, 55]
    [14, 51, 54, 54, 67, 75, 97, 98, 98, 61, 27, 55]
    [14, 51, 54, 54, 61, 67, 75, 97, 98, 98, 27, 55]
    [14, 27, 51, 54, 54, 61, 67, 75, 97, 98, 98, 55]
    [14, 27, 51, 54, 54, 55, 61, 67, 75, 97, 98, 98]
    排序结束
    [14, 27, 51, 54, 54, 55, 61, 67, 75, 97, 98, 98]
    


  • 相关阅读:
    ubuntu如何设置Python的版本
    PHP队列之理论篇
    ubuntu系統如何啟動root用戶登陸?
    如何绑定腾讯企业邮箱?
    VMware虚拟机安装Ubuntu并设置root登陆
    毕业生,如何选择高薪资与学习机会?
    如何改变memcached默认的缓存时间?
    PHP常用函数之数组篇
    如何安装并使用bower包依赖工具
    z-score
  • 原文地址:https://www.cnblogs.com/riasky/p/3464944.html
Copyright © 2011-2022 走看看