package com.heima.work;
public class Demo2_Work {
public static void main(String[] args) {
String line = "25 11 -6 20 102 9";
System.out.println(stringSort(line));
}
public static String stringSort(String line) {
String[] arr = line.split(" ");
int[] arr2 = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
arr2[i] = Integer.parseInt(arr[i]);
}
for (int i = 0; i < arr2.length - 1; i++) {
for (int j = 0; j < arr2.length - i - 1; j++) {
if(arr2[j] > arr2[j+1]) {
int temp = arr2[j];
arr2[j] = arr2[j+1];
arr2[j+1] = temp;
}
}
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr2.length; i++) {
if(i == arr2.length - 1) {
sb.append(arr2[i]);
}else {
sb.append(arr2[i]).append(" ");
}
}
String line2 = sb.toString();
return line2;
}
} |
|