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

    概述

    递增方式插入排序

    代码

    package com.lilei.myes.es.pack1114;
    
    import java.util.Random;
    
    public class insert_sort {
    
    	public static void main(String[] args) {
    
    		Random rand = new Random();
    		
    		int[] array = new int[10];
    		
    		for(int i=0;i<array.length;i++)
    			array[i] = rand.nextInt(100);
    		
    		for(int v:array)
    			System.out.print(v+",");
    		
    		System.out.println();
    		
    		sort(array);
    		
    		for(int v:array)
    			System.out.print(v+",");
    		
    	}
    
    	static void sort(int[] array) {
    
    		for (int i = 1; i < array.length; i++) {
    
    			int p = i - 1;
    			int value = array[i];
    
    			while (p >= 0 && value < array[p])
    			{
    				array[p+1] = array[p];
    				p--;
    			}
    			
    			if (p != i-1)
    				array[p+1] = value;
    
    		}
    
    	}
    
    }
    

      

  • 相关阅读:
    rwkj 1337
    poj 1002
    map
    vector
    sort排序
    sort函数
    poj 2945
    poj2388
    rwkj 1422搜索(素数环)
    poj2503
  • 原文地址:https://www.cnblogs.com/lilei2blog/p/7832649.html
Copyright © 2011-2022 走看看