A function that arranges numbers in a given nonnegative integer array to the maximum number

Source: Internet
Author: User

Recently on the Internet to see such a topic, self-pondering a bit.

Java Version "1.8.0_40"

Write a function that arranges the numbers in a given nonnegative integer array to the maximum number. For example, given [50,2,1,9], the maximum number is 95021. public class Sort {public static void main (String args[]) {int number[] = {1,2,3,32,335,34,7,6,9};int number1[] = {312,321, 3354,222,8}; System.out.println (Sortmax (number)); System.out.println (Sortmax (Number1));} public static String Sortmax (int num[]) {int number[] = new Int[num.length];for (int i = 0; i < num.length; i++) {Number [i] = num[i];} int max = 0;int min = 0; StringBuffer s = new StringBuffer (); for (int i = 0, i < number.length; i++) {for (int j = i + 1; j < Number.length;  J + +) {Integer x = Number[i];integer y = number[j];int tempx = Integer.parseint (x.tostring () + "" + y.tostring ()); int Tempy  = Integer.parseint (y.tostring () + "" + x.tostring ());//Key, convert to string type, then back to int type to compare if (Tempx > Tempy) {max = X;min = Y;} Else{max = Y;min = x;} Number[i] = max;number[j] = min;}} for (int i = 0; i < number.length; i++) {s.append (number[i]);} return s.tostring ();}} /*output:976343353322183354321312222*///: Output String type
The key point of the program is the stitching comparison size. Two for loop nested implementation sorting. x and Y are integer objects. An assignment is made at the beginning of the inner for Loop. I always use number[i] to compare.


If the value is not re-assigned, it can cause unexpected errors. For example, initialize x before the inner for loop.

Error demonstration//non-reinitialization of objects public class Sort {        public static void Main (String args[]) { & nbsp      int number[] = {1,2,3,32,335,34,7,6,9};        int number1[] = { 9,8,55,222,8};        system.out.println (Sortmax (number));         system.out.println (Sortmax (number1));    }        public static String Sortmax (int num[]) {        int number[] = new int[num.length];   & nbsp;    for (int i = 0; i < num.length; i++) {             Number[i] = num[i];        }        int max = 0;         int min = 0;        stringbuffer s = new StringBuffer (); &nb sp;       for (int i = 0;i < number.length; i++) {            integer x = number[i];    //  Put X on the inner for Loop front             for (int j = i + 1; j < Number.length; J + +) {// Diff                 integer y = number[j];                int tempx = Integer.parseint (x.tostring () + "" + Y.tostring ());                int tempy = Integer.parseint (y.tostring () + "" + x.tostring ());                 //key, switch to string type, and then back to int type for comparison              .    if (tempx > Tempy) {                 & nbsp  max = x;                    min = y;                 }                else{                    max = y;                     min = x;            & nbsp;   }                number[i] = max;                 number[j] = min;            }        }        for (int i = 0; i < Number.length; i++) {            s.append (number[i]);        }        Return s.tostring ();    }}/*output:911111111 This is a wrong result 98855222 The result is the correct *///: output String type 
In this error example, after the inner for Loop has run, the integer object y is recycled and the object x contains a value of 1. Number[0] = = 2, number[1] The value of ==1,x is 1. Inside for loop J = 2,

The value of Object X 1 is compared to number[2]. After sorting, number[2] = = 1,number[0] = = 3. And so on, the next value of the array number becomes 1.


A function that arranges numbers in a given nonnegative integer array to the maximum number

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.