插入排序java代码

/**
     * 插入排序
     * 
     * 原理:从数组中取出一个值插入到一个左边已经排好序的数组片段中
     * 
     * @param a
     * @return 
     */
    public long[] InsertSort(long[] a){
        
        int in ;
        int out;
        for(out = 1 ; out < a.length ; out++){
            long temp = a[out];
            in = out;
       /*将temp插入到已经排好序的数组片段中*/

        // 循环左移
while(in > 0 && a[in - 1] >= temp){ a[in] = a[in - 1]; --in; }
        // 把小于temp的数换成temp a[in]
= temp; } return a; }

 

posted @ 2015-09-08 21:33  orlion  阅读(171)  评论(0编辑  收藏  举报