public class FandMax {
public void makeMax(int[] nums){
boolean flag=true;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length-i-1; j++) {
if(compare(nums[j],nums[j+1])){
swap(nums, j, j+1);
flag=false;
}
}
if(flag){
return;
}else {
flag=true;
}
}
}
/**
* 需要交换返回true不需要返回false
* 11 12 不需要交换 2 20 需要交换
*/
public boolean compare(int n1, int n2) {
int temp1=Integer.parseInt(n1+""+n2);
int temp2=Integer.parseInt(n2+""+n1);
if(temp1>temp2){
return true;
}else {
return false;
}
}
/**
* 交换两个数
*/
public void swap(int nums[],int key1,int key2){
int temp;
temp=nums[key1];
nums[key1]=nums[key2];
nums[key2]=temp;
}
public static void main(String[] args) {
int nums[]={2,18,21,45};
FandMax fandMax=new FandMax();
fandMax.makeMax(nums);
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
}