【问题标题】:How to use a bubble sort to sort a multidimensional array in JavaJava中如何使用冒泡排序对多维数组进行排序
【发布时间】:2019-05-12 17:40:18
【问题描述】:

这是我根据运动创建的一个测验,它会询问用户每次尝试 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;
}

【问题讨论】:

  • 我不明白String[][] scoreboard 代表什么。这个数组中有哪些值,它们是什么意思?
  • 用户名在索引 0 上。他们获得的分数在索引 1 上。

标签: java arrays sorting multidimensional-array bubble-sort


【解决方案1】:

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

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

            }
        }
}

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

【讨论】:

    【解决方案2】:

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

    {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];
    

    更易于理解和阅读。

    现在要对该数组进行排序,您可以实现一个自定义类,该类允许您比较两个 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; 
            } 
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 2020-06-26
      • 1970-01-01
      • 2020-07-17
      • 2016-03-03
      • 2020-05-17
      相关资源
      最近更新 更多