0

I have an exercise to sort an array (type int) so I can get the largest number possible.

An example:

1,3,9 ==> 931

1,3,9,60 ==> 96031

So here is my idea: It is impossible to just sort the array to form the number that I wanted. So I can check the first number of each element in array, using the very same idea as bubble sort, just one small difference is that i use the first element to check instead of just arr[i]. But I still want to know beside using my idea, are there any other way (more efficiency). Even if your idea are the very same with my idea but you have something upgrade. Thank you very much

1
  • Well maybe consider looking into the mergesort algorithm it is faster then bubblesort and should work perfectly for your Problem Commented Jan 18, 2021 at 7:13

4 Answers 4

1

It is impossible to just sort the array to form the number that I wanted.

Actually, it isn't impossible.

What you need is to design and implement an ordering that will result in the (decimal) numbers that will make the final number to be largest to sort first; e.g. for the numbers in your question, the ordering is:

9 < 60 < 3 < 1

You just need to work out exactly what the required ordering is for all possible non-negative integers. Once you have worked it out, code a Comparator class that implements the ordering.

Hint: You would be able to specify the ordering using recursion ...

Sign up to request clarification or add additional context in comments.

Comments

1

Solution

For the descending order we multiply here by -1 each value in the array then sort the array and then multiply back with -1. Ultimately we build the result string with string concatenation and print it out

import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
    int[] array = {3,1,9};
    
    for (int l = 0; l < array.length; l++){
        array[l] = array[l]*-1;
    }
    Arrays.sort(array);
    for (int l = 0; l < array.length; l++){
        array[l] = array[l]*-1;
    }
    String res = "";
    
    for(int i = 0; i < array.length; i++){
        res+=array[i];
        
    }
    System.out.println(res);
    }
}

Output

931

Alternatively

Or as @Matt has mentioned in the comments you can basically concat the string in reverse order. Then there is no need anymore for the ascending to descending transformation with *-1

 import java.util.Arrays;

 public class MyClass {
     public static void main(String args[]) {
         int[] array = {
             9,
             1,
             3
         };
         String res = "";

         Arrays.sort(array);
         for (int l = array.length - 1; l >= 0; l--) {
             res += array[l];
         }



         System.out.println(res);
     }
 }

7 Comments

Instead of multiplying it with 1 twice, you could just sort it and then reverse the array before making it a number
@R4yY Ty for the advice but I don't understand it, can you give an example? Instead of doing an reverse to this do with math I thought is maybe interesting for the OP.
You sort the array to get the ascending order and then you reverse the array, for example with Collections.reverse to get the descending order
@R4yY no need to inverse, just run the loop of the string conversion the other way around... for(int i = array.length-1; i >= 0; i--)
@Matt Ohh you hit me with 500 IQ move. Yes what Matt said, do that
|
0

Hope it will work as per your requirement->

 public static void main(String[] args) {
            Integer[] arr = {1,3,3,9,60 };
            List<Integer> flat = Arrays.stream(arr).sorted((a, b) -> findfirst(b) - findfirst(a)).collect(Collectors.toList());
            System.out.println(flat);
    
        }
    
        private static int findfirst(Integer a) {
            int val = a;
            if(val>=10) {
    
                while (val >= 10) {
                    val = val / 10;
                }
            }
            return val;
        }

Comments

0

compare first number of each int then if it is the biggest put it at beginning then you continue, if first char is equal step into the second etc etc the bigest win, if it same at max-char-size. the first selected would be pushed then the second one immediatly after as you already know.

In that maneer 9 > 60 cuz 960>609 the first char will always be the biggest in that case when u concat.

it's > 9 < not > 09 <

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.