码迷,mamicode.com
首页 > 其他好文 > 详细

插入排序

时间:2014-06-26 16:04:32      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   ext   

 

import java.util.*;

public class InsertionSort {
    
    /*
     * 升序排列
     * 
     * */
    public static void insertSort(int[] input) {
        for (int i = 1; i < input.length; i++) {
            int key = input[i];
            int j = i - 1;
            while (j >= 0 && input[j] > key) {
                input[j + 1] = input[j];
                j = j - 1;
            }
            // 每次右移后,j 都要左移,所以要+ 1
            j = j + 1;
            input[j] = key;
        }
    }

    public static void main(String[] args) {

//        int[] input = { 1, 3, 7, 6, 4, 2, 9, 0 };
        
        int len = 10;
        int [] input = new int [len];
        Random random = new Random();
        for(int i = 0; i < len; i ++)
        {
            input[i] = random.nextInt(1000);
        }
        

        insertSort(input);

        for (int i = 0; i < input.length; i++) {
            
            System.out.print(input[i] + "  ");

        }

    }

}

 

插入排序,布布扣,bubuko.com

插入排序

标签:style   class   blog   code   java   ext   

原文地址:http://www.cnblogs.com/kisstherain/p/3808959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!