650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7D/E7/wKiom1byU6DzztKXAAA5_BlSwTo581.png "title=" [ 3rih2@$%fe[k~55@38%l1a.png "alt=" Wkiom1byu6dzztkxaaa5_blswto581.png "/>
The first diagram above illustrates the logic, which is the logic of recursion.
The XY sort is equivalent to inserting an empty slot on the X-y,x with only two slots, and the Y-plug is preceded by a yx,y insert to get XY
The XYZ sort is equivalent to inserting z on all the results of the XY sort, and the result
The code is implemented as follows
public static Void main (String[] args) {list<string> list = sort ("xy", "K"); for (String s: List) {System.out.println (s);}} Public static list<string> sort (String main,string insert) {List<String> result = new ArrayList<> (); if (Main.length () == 1) {Result.add (main+ insert); Result.add (Insert+main);} Else{list<string> lastresult = sort (Main.substring (0,main.length ()-1), main.substring ( Main.length ()-1)); for (String s:lastresult) {int insertindex = 0;while (insertIndex<= S.length ()) {if (insertindex == 0) {result.add (insert+s);} Else if (Insertindex == s.length ()) {Result.add (S+insert);} Else{result.add (s.substring (0, insertindex) +insert+s.substring (Insertindex, s.length ()));} insertindex++;}}} Return result;};
The test result is: ZXY;XZY;XYZ;ZYX;YZX;YXZ;
In addition test ABCD sorting results are: DCAB;CDAB;CADB;CABD;DACB;ADCB;ACDB;ACBD;DABC;ADBC;ABDC;ABCD;DCBA;CDBA;CBDA;CBAD;DBCA;BDCA;BCDA; BCAD;DBAC;BDAC;BADC;BACD;
This article is from the "Cowboy Spring" blog, be sure to keep this source http://syli25.blog.51cto.com/11239954/1754386
Java implementation recursive implementation of string XYZ ordering