Package WMK;
Import Java.util.Scanner;
public class Maopaopaixu {
Public Maopaopaixu () {}
public void Maxtomin () {
Get console input with scanner
Scanner sc = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please input String:");
String s = sc.nextline ();
int len = S.length ();
int ints[] = new Int[len];
into an array
for (int i = 0; i < ints.length; i++) {
Ints[i] = integer.valueof (s.substring (i, i + 1));
}
Core code
Method One: The comparison of two adjacent elements, to the tail of the closer
for (int i = 0; i < ints.length-1; i++) {
for (int j = i + 1; j < Ints.length; J + +) {
if (Ints[i] > Ints[j]) {
int tem;
tem = Ints[i];
Ints[i] = Ints[j];
INTS[J] = tem;
}
}
}
Method Two: A comparison of two elements in the tail and the middle
for (int i = 0; i < ints.length-1; i++) {
for (int j = ints.length-1; j > i; j--) {
if (Ints[i] > Ints[j]) {
int tem;
tem = Ints[i];
Ints[i] = Ints[j];
INTS[J] = tem;
}
}
}
for (int i = 0; i < ints.length; i++) {
System.out.print (Ints[i]);
}
}
public static void Main (string[] args) {
Maopaopaixu m = new Maopaopaixu ();
M.maxtomin ();
}
}
Java Bubble sort