冒泡按描述/升序排序从数组中排序字符串数据

时间:2014-03-31 20:54:23

标签: java arrays sorting bubble-sort

在数组中排序字符串时遇到了麻烦。

我应该从我的代码中获得两个不同的输出,但我只能按升序获得输出。

    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;
    }
}

解释为什么它只按升序排序,将会更有帮助。

提前致谢。

2 个答案:

答案 0 :(得分:1)

我认为bubbleSortDesc按升序排序,而BubbleSortAsc根本不起作用:

            //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;
            }

所以,你应该再次使用BubbleSortAsc:

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

在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.

答案 1 :(得分:0)

实际上你真的很难

对于冒泡排序:

另外我猜你做得非常困难,你可以对它们进行排序并打印出来:

    ...
    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]);
    }
    ...

希望这有帮助