首页 > 其他 > 详细

插入排序

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

 

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

插入排序

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!