All rights reserved. All rights reserved.
Welcome reprint, please indicate the source when reproduced:
http://blog.csdn.net/xiaofei_it/article/details/51524798
To prevent rigid thinking, brush an algorithmic question every day. It has been brushed for a few days now, send point code.
I have built an open source project, and every day the topic is in it:
Https://github.com/Xiaofei-it/Algorithms
Most of the algorithms are written by myself, without reference to the online generic code. The reader may find the code obscure because it is my own understanding.
The last few days have been writing something original, mostly non-recursive. In the future, ready to brush points DP, greedy and other problems.
The following is a recursive non-recursive method for fast sequencing.
Is there no man like me in such a painful way to write a quick sort of non-recursive???
/** * * Xiaofei * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicable Law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warra Nties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * */package xiaofei.algorithm;import java.util.stack;/** * Created by Xiaofei on 16/5/25. * */public class QuickSort {private static int[] array; private static void sortcorecursively (int l, int r) {class Element {int l; int R; int i; int state; Here the state is used to represent the general status, unlike the previous non-recursive code style. Element (int l, int r) {THIS.L = l; THIS.R = R; THIS.I =-1; this.state = 0; }} stack<element> Stack = new stack<> (); Stack.push (New Element (L, R)); while (!stack.isempty ()) {element element = Stack.peek (); if (element.state = = 0) {int i = ELEMENT.L, j = element.r, M = array[(i + j)/2]; Do {while (Array[i] < m) {++i; } while (M < array[j]) {--j; } if (I <= j) {int temp = Array[i]; Array[i] = Array[j]; ARRAY[J] = temp; ++i; --j; }} while (I <= j); ELEMENT.I = i; if (Element.l < J) {Stack.push (new element (ELEMENT.L, j)); } element.state = 1; } else if (element.state = = 1) {if (Element.i < ELEMENT.R) {Stack.push (new Element) ( ELEMENT.I, ELEMENT.R)); } element.state = 2; } else if (element.state = = 2) {stack.pop (); }}} public static void sort (int[] array) {Quicksort.array = array; sortcorecursively (0, array.length-1); }}
Brush one algorithm per day 20160525: Recursive non-recursive method for fast sequencing