简体   繁体   中英

How can I bubble sort large numbers in an array?

My Question is-

An array of String, nums, has been provided as input. Each String represents a non negative integer. The integers can be very large and hence have been stored as strings. The array has to be sorted in ascending order of the integer value of the strings. bubbleSort({"999","723534","99","18456543876","54253445340001","98","112343",})

Expected Output is-{"98","99","999","112343","723534","18456543876","54253445340001"}

Actual Output is-{"99","98","999","112343","723534","18456543876","54253445340001"}

I dont know hot to compare same digit large numbers.

public class BubbleSortLargeNums {

    static String[] testcase1 = {"999","723534","99","18456543876","54253445340001","98","112343",};
    //static String[] testcase1 = {"1"};

    public static void main(String args[]){
        BubbleSortLargeNums testInstance = new BubbleSortLargeNums();
        String[] result = testInstance.bubbleSort(testcase1);
        System.out.print("{");
        for (int i=0;i<result.length;i++){
            if (i>0)
                System.out.print(",");
            System.out.print('"'+result[i]+'"');
        }
        System.out.println("}");
    }

    //write your code here
    public String[] bubbleSort(String[] arr){
        int j=0;
        int prevI=0;
        for(int i=0;i<arr.length-1;i++){
            j++;
            if(j<arr.length){
                i=prevI;
            }
            else{
                j=i+1;
            }
            if(arr[i].length()>arr[j].length()){
                String temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
            prevI=i;
        }
        return arr;
    }
}

The problem is that you're comparing your String s by their length:

if (arr[i].length()>arr[j].length()) { //here's the problem!
    String temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
}

Your best bet would be converting these String s into a number representation, like long :

long elem1 = Long.parseLong(arr[i]);
long elem2 = Long.parseLong(arr[j]);
if (elem1 > elem2) {
    String temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
}

If the numbers are really huge, greater than long max value, 9223372036854775807, then use BigInteger instead:

BigInteger elem1 = new BigInteger(arr[i]);
BigInteger elem2 = new BigInteger(arr[j]);
if (elem1.compareTo(elem2) > 0) {
    String temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
}

Firstly short the elements length wise

if (arr[i].length()>arr[j].length()) { 
String temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}

Now the problem will be with same length elements eg 98 and 99 .use compareTo operator swap when the result is > 1

if j > j+1 swaps the numbers eg "99".compareTo."98" =1

First digit's difference is calculated but if the first digits are same next digit is compared and difference is calculated(left--->right)

if(str[j].length() == (str[j+1]).length()){ 
if ( str[j].compareTo(str[j+1])>0 ){
String temp = str[j];               
str[j] = str[j+1];
str[j+1] = temp;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM