"Suning easy Buy pen" bubble sort the number "1492586" is sorted into "9865421" and then a new string is generated.

Source: Internet
Author: User

 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.