Interview question 3 (string sorting)

Source: Internet
Author: User

Sort the number of integers in an integer array. The rule is first to compare the first digit, and then to the second digit if the number is equal. For example, 22,123, 12,123, and 2 are sorted by, and 44.


Public class test01 {
Public static void main (String [] args ){
Int [] str = {1212,123,132 };
Maopao (str );
}
Public static void maopao (int str []) {
Int temp = 0;
For (int I = 0; I
For (int j = 0; j
// This loop implements Bubble sorting, but the sorting rules are different.
Int s1 = (str [j] + ""). length (); // The length of the previous number
Int s2 = (str [j + 1] + ""). length (); // The length of the last number
If (s1> s2) {// if the length of the first number is greater than that of the second number, for example, 123 and 22
String splitLong = (str [j] + ""). substring (0, s2); // split the long one into the same length as the short one.
/**
* The principle is to compare the length of the previous number and the last number. If the length is the same, it is a big positive.
* If the backend is different, it is split into the same comparison.
*/
If (Integer. parseInt (splitLong)> = Integer. parseInt (str [j + 1] + "")){
// If the long split number is larger than the shorter one, bubble sort is performed.
Temp = str [j];
Str [j] = str [j + 1];
Str [j + 1] = temp;
}
} Else if (s1 String splitLong = (str [j + 1] + ""). substring (0, s1 );
If (Integer. parseInt (splitLong)
Temp = str [j];
Str [j] = str [j + 1];
Str [j + 1] = temp;
}
} Else {// This is equal
If (Integer. parseInt (str [j] + "")> Integer. parseInt (str [j + 1] + "")){
Temp = str [j];
Str [j] = str [j + 1];
Str [j + 1] = temp;
}
}
}
For (int x = 0; x
System. out. print (str [x] + "");
}
System. out. println ();
}
For (int I = 0; I
System. out. print (str [I] + "#");
}
}

}

I don't know if there are any simpler methods?








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.