为什么我的冒泡排序不起作用?Java

4

首次发布! 我有一个作业要求我编写一个Java程序,从文本文件中读取数据并按照分数和缩写排序。 文本文件如下:

John Doe 75
Joe Blow 65
Mary Smith 80
John Green 82
Jill White 97

这是我的代码:
import java.util.Scanner;
public class HelloWorld{
public static void main(String[] args) throws Exception{
    String[] firstName = new String[5];
    String[] lastName = new String[5];
    int score[] = new int[5];
    java.io.File file = new java.io.File("data.txt");
    Scanner input = new Scanner(file);

        int c=0;
        while(input.hasNext()){
            firstName[c] = input.next();
            lastName[c] = input.next();
            score[c] = input.nextInt();
            c++;
        }
        input.close();

    MichaelBubbleSort(score);
    for(int x=4;x>=0;x--){
        System.out.print(firstName[x].substring(0,1) + lastName[x].substring(0,1) + " " + score[x]);
        System.out.println();
    }
}


public static void MichaelBubbleSort(int[] arr){
    int temp;
    for(int i=0; i < arr.length-1; i++){

        for(int j=1; j < arr.length-i; j++){
            if(arr[j-1] > arr[j]){
                temp=arr[j-1];
                arr[j-1] = arr[j];
                arr[j] = temp;
       }
     }
   }
 }

但是出现了某些原因,它总是呈现为:

JW 97                                                                                                                                                            
JG 82                                                                                                                                                            
MS 80                                                                                                                                                            
JB 75                                                                                                                                                            
JD 65  

输出应该是:
JW 97
JG 82
MS 80
JD 75
JB 65

这种情况发生的原因是什么?

您已经对分数进行了排序,但没有相应地对姓名进行排序。这就是您遇到此问题的原因。 - Zealous System
你会期望哪种输出?(请修改问题) - xerx593
2个回答

2
这是根据您的代码提供的解决方案。但是,代码必须比这更好。您应该使用集合类。 在此代码中,您需要相应地交换名称。
import java.util.Scanner;

public class HelloWorld{
    public static void main(String[] args) throws Exception {
        String[] firstName = new String[5];
        String[] lastName = new String[5];
        int score[] = new int[5];
        java.io.File file = new java.io.File("D:\\test.txt");
        Scanner input = new Scanner(file);

        int c = 0;
        while (input.hasNext()) {
            firstName[c] = input.next();
            lastName[c] = input.next();
            score[c] = input.nextInt();
            c++;
        }
        input.close();

        MichaelBubbleSort(score,firstName,lastName);
        for (int x = 4; x >= 0; x--) {
            System.out.print(firstName[x].substring(0, 1)
                    + lastName[x].substring(0, 1) + " " + score[x]);
            System.out.println();
        }
    }

    public static void MichaelBubbleSort(int[] arr,String[] firstName, String[] lastName) {
        int temp;
        String tempFirstName,tempLastName;
        for (int i = 0; i < arr.length - 1; i++) {

            for (int j = 1; j < arr.length - i; j++) {
                if (arr[j - 1] > arr[j]) {
                    temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;

                    tempFirstName = firstName[j -1];
                    firstName[j - 1] = firstName[j];
                    firstName[j] = tempFirstName;

                    tempLastName = lastName[j -1];
                    lastName[j - 1] = lastName[j];
                    lastName[j] = tempLastName;
                }
            }
        }
    }
}

非常感谢!我本来以为排序会同时处理名字和姓氏。但是我们现在不允许使用集合,因为它们不在我们正在学习的章节中。不过还是谢谢你的提示。 :-) - Joe

2

出现这种情况是因为你没有按照分数对名称进行排序。我建议使用对象数组存储数据,然后使用冒泡排序对对象数组进行排序,像这样:

public class B{

public static void MichaelBubbleSort(Person[] arr){
    Person temp;
    for(int i=0; i < arr.length-1; i++){

        for(int j=1; j < arr.length-i; j++){
            if(arr[j-1].score > arr[j].score){
                temp=arr[j-1];
                arr[j-1] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

public static void main(String x[]){
    Person [] a={new Person("John","Doe",75),new Person("Joe","Blow",65),new Person("Mary","Smith",80),new Person("John","Green", 82),new Person("Jill","White", 97)};
    MichaelBubbleSort(a);

    for(Person i:a){
        System.out.println(i.FirstName+" "+i.LastName+" "+i.score);
    }

}
}

数据存储的对象类

class Person{
    int score;
    String FirstName;
    String LastName;

    Person(String FName,String LName,int Score){
        this.score=Score;
        this.FirstName=FName;
        this.LastName=LName;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接