package com.heima.stringbuffer;
import java.util.Date;
public class Demo4_Sort {
/**
* @param args
* (1)有一个数字字符串"25 11 -6 20 102 9",数字之间用空格间隔
(2)利用冒泡排序对这串数字按照从小到大排序,生成一个数值有序的字符串"-6 9 11 20 25 102"
(3)提示:用String类提供的split方法(用" "(空格)作为split方法的参数来切割),然后生成对应的数字字符串数组
*/
public static void main(String[] args) {
DEMO();
}
static void show(StringBuffer sb,String str){
sb.append(str);
}
static void show(String s1,String s2){
s1 = s2+s1+"Q";
s2 = "W"+s1;
}
private static void DEMO() {
String str = "25 11 -6 20 102 9";
String[] arr = str.split(" ");
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - (i + 1); j++) {
if (Integer.parseInt(arr[j]) > Integer.parseInt(arr[j+1])) {
String temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
if (i != arr.length -1) {
sb.append(arr[i]).append(" ");
}else {
sb.append(arr[i]);
}
}
String str1 = sb.toString();
System.out.println(str1);
}
}
|
|