插入排序2

来源:互联网 发布:qt4 串口源码 编辑:程序博客网 时间:2024/06/07 03:46
/** * 插入排序的递归实现: * 使用递归的方法代替循环遍历 * Created by hasee on 2017/6/8. */public class InsertSort2 {    public static void doSort(int[] a){        if(a.length>2){            doSort(Arrays.copyOf(a,a.length-1));        }        int next = a[a.length - 1];        int i = a.length-2;        while (i>=0 && a[i]>=next){            a[i+1] = a[i];            i--;        }        a[i+1] = next;    }    public static void main(String[] args) {        int[] a = {1,2,3,4,5,6,7,8,9};        doSort(a);        for(int i=0; i<a.length; i++){            System.out.println(a[i]);        }    }}