【java数据结构】快排非递归方式

递归太深(数据量过大,100万以上)会出现栈满的问题,改用非递归:

public static void quickSortByMoreData(int arr[]) {
        int left = 0;
        int right = arr.length - 1;
        Stack stack = new Stack();
        stack.push(left);
        stack.push(right);
        while(!stack.isEmpty()) {
            int r = stack.pop();
            int l = stack.pop();
            int s = getStart(arr, l, r);
            if(s-1 > l) {
                stack.push(l);
                stack.push(s-1);
            }
            if(s+1 < r) {
                stack.push(s+1);
                stack.push(r);
            }
        }

你可能感兴趣的:(Java)