The bubble sort algorithm sorts the numeric string and returns the sorted string __ algorithm

Source: Internet
Author: User
Tags numeric sorts stringbuffer
Package Com.tianwen;

public class Wzhtest {

/**
* @param args
*/
public static void Main (string[] args) {

String str= "2,1,0,3";
Str= "A2-b1,a0-b2,a1-b3,a3-b0";
String Newstr=sortstringtoarray (str);
System.out.println ("sorted string:" +newstr);

}

/**
* Bubble sort algorithm sorts numeric strings and returns the sorted string
* @param str
* @return
*/

public static string Sortstringtoarray (String str) {
if (null==str| | "". Equals (str)) {
return str;
}
String[] Arrstr=str.split (",");
if (arrstr.length<2) {
return str;
}
if (!isnum (Arrstr[0])) {
return str;
}
String temp;
for (int i = 0; i < arrstr.length; i++) {
for (int j = arrstr.length-1; j > i; j--) {
if (integer.valueof (Arrstr[i]) >integer.valueof (Arrstr[j])) {
temp = Arrstr[i];
Arrstr[i] = Arrstr[j];
ARRSTR[J] = temp;
}
}
}
StringBuffer sb=new StringBuffer ();
for (int k=0;k<arrstr.length;k++) {
Sb.append (Arrstr[k]);
if (k<arrstr.length-1) {
Sb.append (",");
}
}
return sb.tostring ();
}

public static Boolean isnum (String str) {
Return Str.matches ("^[-+]?" ( ([0-9]+) ([.] ([0-9]+))? | ([.] ([0-9]+)]?) $");
}
}

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.