Question

I've run into trouble when sorting string in an array.

I'm supposed to get two different outputs from my code, but I only get the output in ascending order.

    import java.util.*;
public class nextLineArray1
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String names[]=new String[12];
        System.out.println("Enter the 12 names: ");
        //Load Array
        for(int i = 0; i < 12; i++)
        {
            names[i] = input.nextLine();

        }

        //Print descending order list
        String[] descSort;
        descSort=bubbleSortDesc(names);
        System.out.println("Names listed sorted in descending order (via BubbleSort): ");
        for(int x=0; x < names.length; x++)
        {
            System.out.println(names[x]);
        }
        //Print ascending order list
        String[] ascSort;
        ascSort=bubbleSortAsc(names);
        System.out.println("Names listed sorted in ascending order (via BubbleSort): ");
        for(int z=0; z < names.length; z++)
        {
            System.out.println(names[z]);
        }
    }
    public static String[] bubbleSortDesc(String[] names)
    {
        String temp;
        int passNum, i, result;
        for(passNum=1; passNum <= 11; passNum++)
        {
            for(i = 0; i<=(11-passNum); i++)
            {
                result=names[i].compareToIgnoreCase(names[i+1]);
                if(result>0)
                {
                    temp=names[i];
                    names[i]=names[i+1];
                    names[i+1]=temp;
                }
            }
        }
        return names;

    }

    public static String[] bubbleSortAsc(String[] names)
    {
        String temp;
        int passNum, i, result;
        for(passNum=1; passNum <= 11; passNum++)
        {
            for(i = 0; i<=(11-passNum); i++)
            {
                result=names[i].compareToIgnoreCase(names[i]);
                if(result>0)
                {
                    temp=names[i-1];
                    names[i-1]=names[i];
                    names[i]=temp;
                }
            }
        }
        return names;
    }
}

An explanation of why it is only sorting in ascending order, would be more than helpful.

Thanks in advance.

Was it helpful?

Solution

Looks to me like bubbleSortDesc is sorting in ascending order, and BubbleSortAsc doesn't work at all:

            //In BubbleSortAsc
            result=names[i].compareToIgnoreCase(names[i]);
            //result == 0, since names[i] is definitely equal to names[i]
            if(result>0)
            {
                //Never reached
                temp=names[i-1];
                names[i-1]=names[i];
                names[i]=temp;
            }

So, you should have, again in BubbleSortAsc:

            result=names[i].compareToIgnoreCase(names[i+1]);
            if(result>0) //names[i] > names[i+1], so swap them.

And in BubbleSortDesc:

            result=names[i].compareToIgnoreCase(names[i+1]);
            if(result<0) // This was backwards.  If names[i] < names[i+1], you need to swap, for descending order.

OTHER TIPS

Actually you're making it really hard

For booble sort:

Also I guess you're making it very difficult, you can sort them and print them like this:

    ...
    for(int i = 0; i < (names.length - 1); i++){
        for(int j = i; j < (names.length - 1); j++){
            if(names[j].compareToIgnoreCase(names[i]) > 0){
                String aux = names[i];
                names[i] = names[j];
                names[j] = aux;
            }
        }
    }
    for(int i = 0; i < names.length; i++){
        System.out.println(names[i]);
    }
    for(int i = 0; i < (names.length - 1); i++){
        for(int j = i; j < (names.length - 1); j++){
            if(names[j].compareToIgnoreCase(names[i]) < 0){
                String aux = names[i];
                names[i] = names[j];
                names[j] = aux;
            }
        }
    }
    for(int i = 0; i < names.length; i++){
        System.out.println(names[i]);
    }
    ...

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top