算法学习之插入排序

插入排序的基本思想:向后遍历把遍历到的元素与前面每一个元素比较 

package StudentSufa;
/*
直接排序
 */
public class InsertSort {
    public static void main(String[] args){
        int[] a={49,38,65,97,23,22,76,1,5,8,2,0,-1};
        //直接排序开始
        for (int i = 1; i <a.length ; i++) {
            //取出新遍历到的值与前面的所有值进行比较把最小的放在最前面
            int temp=a[i];
            int j;
            for (j=i-1;j>=0;j--){
                if(a[j]>temp){
                    //如果当前的的a[j]大于j当前取出来的值
                    a[j+1]=a[j];
                }else {
                    break;
                }
                a[j+1]=temp;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/microopithecus/article/details/83998174