直接插入排序(JAVA实现)

来源:互联网 发布:mac os dmg镜像下载 编辑:程序博客网 时间:2024/06/05 06:51
/** * Created by DELL on 2017/4/23. * 直接插入排序 */public class InsertSort {    public static void main(String[] args){        int[] in = {33,12,25,46,33,68,19,80};        int count = in.length;        int  j = 0;        InsertSortTest(in);        for(int i=0;i<count;i++){            System.out.print(in[i]+" ");        }    }    public static void InsertSortTest(int[] in){        int temp = 0;//O(1)的辅助空间        int j=0;        for(int i=1;i<in.length;i++){            if(in[i]<in[i-1]){                temp=in[i];                for( j=i-1;j>=0 && in[j]>temp;j--){                    in[j+1] = in[j];                }                in[j+1] = temp;            }        }    }

}

0 0