Public classBubble { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString initial = "1492586"; StringBuffer SB=NewStringBuffer (""); int[] Array =New int[Initial.length ()]; for(inti = 0; I < initial.length (); i++) {Array[i]=Integer.parseint (string.valueof (Initial.charat (i))); } //System.out.print (arrays.tostring (array)); for(inti = 0; I < Initial.length ()-1; i++) { for(intj = 0; J < Initial.length ()-1-i; J + +) { if(Array[j] < Array[j + 1]) { inttemp =Array[j]; ARRAY[J]= Array[j + 1]; Array[j+ 1] =temp; } } } //System.out.print (arrays.tostring (array)); for(inti = 0; i < Array.Length; i++) {sb.append (array[i]); } String out=sb.tostring (); System.out.print (out); }}
Run Result: 9865421
Using ArrayList and collections
Importjava.util.ArrayList;Importjava.util.Collections; Public classBuble { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString in = "1492586"; StringBuffer Sbuffer=NewStringBuffer (""); ArrayList<Integer> List =NewArraylist<integer>(); for(inti = 0; I < in.length (); i++) {List.add (Integer.parseint (string.valueof (In.charat (i))); } collections.sort (List); Collections.reverse (List); System.out.println (List); for(inti = 0; I < in.length (); i++) {sbuffer.append (List.get (i)); } String out=sbuffer.tostring (); System.out.print (out); }}
Operation Result:
[9, 8, 6, 5, 4, 2, 1]
9865421
There should be some other places to simplify ...
"Suning easy Buy pen" bubble sort the number "1492586" is sorted into "9865421" and then a new string is generated.