publicstaticvoidquickSort2(int[] numbers) { /** * 非递归算法,用栈来存储还未排序的片段,然后当只剩当前 一个元素时从栈中取数据,直到栈空 */ ints=0; inte= numbers.length - 1; Stack<Integer> start = newStack<>(); Stack<Integer> end = newStack<>(); int tmp; int i; int j;
while (s < e || !start.isEmpty()) { if (s < e) { tmp = numbers[s]; i = s; j = e; while (i < j) { while (i < j && numbers[j] >= tmp) { j--; } numbers[i] = numbers[j]; while (i < j && numbers[i] <= tmp) { i++; } numbers[j] = numbers[i]; } numbers[i] = tmp; start.add(i + 1); end.add(e); e = i - 1; } elseif (!start.isEmpty()) { s = start.pop(); e = end.pop(); } } }