java快速排序

来源:互联网 发布:java中的private 编辑:程序博客网 时间:2024/06/04 00:35
public class Text {    public static void main(String[] args) {        //递归 1*2*3*4*5//        f(5);//        System.out.println(f(5));        int[] a = {1, 6, 3, 8, 45, 23, 4, 423, 76, 2};        quickSorts(a, 0, a.length - 1);        for (int x : a) {            System.out.print(x + "  ");            //输出结果        }    }    /*private static int f(int x) {        //递归结束条件,否则成死循环        if (x == 1) {            return 1;        } else {            //自己调用自己5*4*3*2*1            return x * f(x - 1);        }    }*/    private static void quickSorts(int[] a, int start, int end) {        if (start < end) {            int base = a[start];            int temp;            int i = start, j = end;            do {                while ((a[i] < base) && i < end)                    i++;                while ((a[j] > base) && j > start)                    j--;                if (i <= j) {                    temp = a[i];                    a[i] = a[j];                    a[j] = temp;                    i++;                    j--;                }            } while (i <= j);            if (start < j) {                quickSorts(a, start, j);            }            if (end > i) {                quickSorts(a, i, end);            }        }    }}
原创粉丝点击