如何使用冒泡排序对 Java 中的多维数组进行排序

How to use a bubble sort to sort a multidimensional array in Java

这是我创建的一个基于运动的测验,它会询问用户每次尝试 3 次的一系列问题。它从那里汇总每个玩家的分数并以二维数组的形式显示,它比较分数并打印最高分数。我将如何使用冒泡排序(不是 array.sort)按第二个索引(分数)对二维数组(记分牌)进行排序。

import java.util.*;

class miniproj
{
  public static void main(String[] args)
  {
    Questions[] questions = setQuestions(); // store array of questions using setquestions method
    askQuestion(questions); // run method askquestion using questions parameter (array)
  }

  public static Questions[] setQuestions()
  {
   Questions[] questions = new Questions[4]; //create array of type questions
    Questions A = new Questions(); // create new questons type called A
    A.question = "What team won the world cup in 1966?";
    A.options = " A. Germany\n B. France\n C. England\n D. Wales";
    A.answer = "C";
    questions[0] =  A; // the first question in the aray is A

    Questions B = new Questions();
    B.question = "Who are the current EPL title holders?";
    B.options = " A. Arsenal\n B. Bournemouth\n C. Chelsea\n D. Manchester City";
    B.answer = "D";
    questions[1] =  B;

    Questions C = new Questions();
    C.question = "Who is the current Golden Boot holder 2017/18 season?";
    C.options = " A. Lionel Messi\n B. Harry Kane\n C. Cristiano Ronaldo\n D. Davidson Sanchez";
    C.answer = "A";
    questions[2] =  C;

    Questions D = new Questions();
    D.question = "Which team has most goals";
    D.options = " A. Arsenal\n B. Bournemouth\n C. Chelsea\n D. Manchester City";
    D.answer = "A";
    questions[3] =  D;

    return questions; // return array of questions
  }

  public static void askQuestion(Questions[] array)
  {
    int correct = 0;
    Scanner sc = new Scanner(System.in);
    String[][] scoreboard = new String[4][2];



    for(int m = 0; m < scoreboard.length; m++) {
      correct = 0;
      System.out.println("What is your name");
      scoreboard[m][0] = sc.nextLine();



        for(int i = 0; i < 4; i ++) // this loops for all four questions
        {

            for(int k = 4; k > 1; k --)
            {
                System.out.println(array[i].question);
                System.out.println(array[i].options);
                String answer = sc.nextLine();

                if(answer.equalsIgnoreCase(array[i].answer)) // this loops til correct answer is given
                {
                  System.out.println("Correct");
                  correct = correct + 1;
                  break;
                }
                else
                {
                  System.out.println("Incorrect, you have " + (k - 2) + " tries left");
                }
            }

        }  

         scoreboard[m][1] = Integer.toString(correct);
         System.out.println(correct + "  questions correct");

    }


    int mostCorrectIndex = 0;

    for (int c = 1; c < scoreboard.length; c++) {
        if (Integer.parseInt(scoreboard[c][1]) > Integer.parseInt(scoreboard[mostCorrectIndex][1]))
            mostCorrectIndex = c;
} 
      System.out.println("The person with the highest score is " + scoreboard[mostCorrectIndex][0]);
      for (int b = 0; b < scoreboard.length; b++) {
        System.out.println(Arrays.toString(scoreboard[b]));
      }
    }
}
class Questions
{
  String question;
  String options;
  String answer;
}

如果我没理解错的话,你有一个像这样的二维数组结构:

{name,score}
{name,score}
{name,score}
{name,score}

并且您想根据第二列:得分对数据进行排序。

与其在二维数组中实现它,不如创建一个名为 Player 的对象

其中 Player 有一个实现:

public class Player{

    private String name;
    private int score;        

    Player(String name){
        this.name = name;
    }

    public void setScore(int score){
        this.score = score;
    }

    public int getScore(){
        return score;
    }
}

现在您的计分板可以像这样实现为一维数组

Player[] scoreboard = new Player[playerSize];

更容易理解和阅读。

现在要对这个数组进行排序,您可以实现一个自定义 class,它允许您比较两个 Player

类型的对象
class comparePlayer implements Comparator<Player>{

    public int compare(Player a, Player b) {
        if (a.getScore() < b.getScore()){
            return -1;
        }
        else if (a.getScore() == b.getScore()){
            return 0;
        }
        else{
            return 1;
        }
    }

}

现在您可以像这样按分数排序 ->

Arrays.sort(scoreboard,new comparePlayer());

或者如果你真的想使用冒泡排序那么你可以这样实现它:

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

在这种情况下,您可以通过修改来实施冒泡排序,以比较重要的值。示例:

static void bubbleSort(String[][] arr) {
        int arrayLength = arr.length;
        for (int i = 0; i < arrayLength; i++) {
            for (int j = 1; j < (arrayLength - i); j++) {
                String nameTemp, scoreTemp;
                int leftValue, rightValue;
                leftValue = Integer.valueOf(arr[j - 1][1]);
                rightValue = Integer.valueOf(arr[j][1]);
                if (leftValue > rightValue) {
                    //swap elements
                    nameTemp = arr[j - 1][0];
                    scoreTemp = arr[j - 1][1];
                    arr[j - 1][0] = arr[j][0];
                    arr[j - 1][1] = arr[j][1];
                    arr[j][0] = nameTemp;
                    arr[j][1] = scoreTemp;
                }

            }
        }
}

然后好吧,你需要数组的最后一个索引,因为它是升序排列的。