java插入排序

来源:互联网 发布:mysql远程登录失败 编辑:程序博客网 时间:2024/06/05 19:33
public class Text {    public static void main(String[] args) {        int[] a = {1, 6, 3, 8, 45, 23, 4, 423, 76, 2};        sorts(a);    }    private static void sorts(int[] a) {        int temp, j;        int size = a.length;        //从第二个元素开始循环        for (int i = 1; i < size; i++) {            //把当前要比较的元素放入temp            temp = a[i];            for (j = i; j > 0 && temp < a[j - 1]; j--) {                //元素集体后移                a[j] = a[j - 1];            }            a[j] = temp;        }        for (int i : a) {            System.out.print(i + "  ");            //输出结果:  1  2  3  4  6  8  23  45  76  423          }    }}