Search code examples
javaarrayssortingmultidimensional-arraybubble-sort

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


Here is is a quiz I created based on sports that asks a series of questions where the user has 3 tries each time. From there it totals up the score of each player and shows it in the form of a 2-d array, it compares the scores and prints the highest score. How would I use a bubble sort ( not array.sort) to sort the 2-d array (scoreboard) by the second index ( the score).

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

Solution

  • If I understand you correctly, you have a 2d array structure like this:

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

    and you want to sort the based on the second column: score.

    Rather than implementing it in a 2D array, why don't you create an object called Player

    where Player has an implementation:

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

    Now your scoreboard can now be implemented into an one dimensional array like this:

    Player[] scoreboard = new Player[playerSize];
    

    Much simpler to understand and read.

    Now to sort this array, you can implement a custom class which allows you to compare two objects of type 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;
            }
        }
    
    }
    

    Now you can sort by score like this ->

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

    Or if you really wanted to use bubble sort then you can implement it like this:

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