Java 字符串冒泡排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19214447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:07:37  来源:igfitidea点击:

Java String Bubble Sorting

javasortingbubble-sort

提问by user2844549

I need help sorting this array in alphabetical order using the bubble sort algorithm.

我需要帮助使用冒泡排序算法按字母顺序对这个数组进行排序。

My code is:

我的代码是:

public class Strings
{
    public static void main(String[] args)
    {
        Scanner reader = new Scanner(System.in);
        String tempStr;


        System.out.print("Enter the strings > ");
        String s1 = new String(reader.nextLine());

        String[] t1 = s1.split(", ");

        for (int t=0; t<t1.length-1; t++)
        {
           for (int i = 0; i<t1.length -1; i++)
           {
               if(t1[i+1].compareTo(t1[1+1])>0)
               {
                   tempStr = t1[i];
                   t1[i] = t1[i+1];
                   t1[i+1] = tempStr;
                }
            }

        }

        for(int i=0;i<t1.length;i++)
        {
            System.out.println(t1[i]);
        }
    }
}

The code compiles, but it does not sort alphabetical. Please help me.

代码可以编译,但不按字母顺序排序。请帮我。

回答by Matthew S.

You have three errors in your code.

您的代码中有三个错误。

The first error is in the inner for loop, in the place where you do the check statement, it should be i < t1.length - t -1not i < t1.length -1. You subtract t because you do not want to loop through the whole array again, only the first part of it.

第一个错误在内部 for 循环中,在执行 check 语句的地方,它应该i < t1.length - t -1不是i < t1.length -1。您减去 t 是因为您不想再次遍历整个数组,只遍历它的第一部分。

The second and third errors are in the if statement. You need to turn the greater than symbol into a lesser than symbol, because the way you have the compareTo method set up, it will return a negative number.

第二个和第三个错误在 if 语句中。您需要将大于号变成小于号,因为设置 compareTo 方法的方式将返回一个负数。

The other error in this line is that in the compareTo parameter you put 1 + 1it actually should be just i, because you want one less than the object it is comparing to.

这一行中的另一个错误是,在 compareTo 参数中,您1 + 1实际上应该将它设置为 just i,因为您希望比它正在比较的对象少一个。

The fixed working code is below (Comments are what you originally had):

固定的工作代码如下(评论是您最初拥有的):

   public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String tempStr;

        System.out.print("Enter the strings > ");
        String s1 = new String(reader.nextLine());

        String[] t1 = s1.split(", ");

        for (int t = 0; t < t1.length - 1; t++) {
            for (int i= 0; i < t1.length - t -1; i++) {
                if(t1[i+1].compareTo(t1[i])<0) {
                    tempStr = t1[i];
                    t1[i] = t1[i + 1];
                    t1[i + 1] = tempStr;
                }
            }
        }
        for (int i = 0; i < t1.length; i++) {
            System.out.println(t1[i]);
        }
   }

回答by JJTargaryen

please change

请更换

String[] t1 = s1.split(", "); 

to

String[] t1 = s1.split("");

This will solve the issue.

这将解决问题。