快排非递归

来源:互联网 发布:unity3d格式 解包 编辑:程序博客网 时间:2024/06/07 19:55
private static void nonRec_quickSort(int[] a,int start,int end) {        LinkedList<Integer> stack = new LinkedList<Integer>();  //用栈模拟        if(start < end) {            stack.push(end);            stack.push(start);            while(!stack.isEmpty()) {                int l = stack.pop();                int r = stack.pop();                int index = partition(a, l, r);                if(l < index - 1) {                    stack.push(index-1);                    stack.push(l);                }                if(r > index + 1) {                    stack.push(r);                    stack.push(index+1);                }            }        }    }        private static int partition(int[] a, int start, int end) {        int pivot = a[start];        while(start < end) {            while(start < end && a[end] >= pivot)                end--;            a[start] = a[end];            while(start < end && a[start] <= pivot)                start++;            a[end] = a[start];        }        a[start] = pivot;        return start;    }
原创粉丝点击