Search code examples
javaarrays2dbubble-sort

2D bubble sort java program that have string and integer in array


I don't know where the problem is. When compiling, it doesn't sort the numbers. What should I do? Badly needed in output in our school. Thanks in advance!

public class Qwerty {
  static void bubbleSort(String arr[][]) {
  int n = arr.length;
  int temp = 0;
  int []myAge = new int[n];

  for(int i = 0; i < n; i++) {
     for(int j=1; j < n-i-1; j++) 
     {
        myAge[j] = Integer.parseInt(arr[j][1]);
        if(myAge[j-1] > myAge[j]) {
           temp = myAge[j-1];
           myAge[j-1] = myAge[j];
           myAge[j] = temp;
        }
     }
  }
}
public static void main(String[] args) {

   Qwerty bs = new Qwerty();
   String arr [][] = {{"Ace","10"},
                     {"Ben","8"},
                     {"Cid","20"},
                     {"Dan","5"}, 
                     {"Eve","12"}};
   bs.bubbleSort(arr);
   for(int i = 0; i < arr.length; i++) {
     System.out.println(arr[i][0]+arr[i][1]);
   }
}

Solution

  • This should work. I'll go through the few mistakes you had in your code.

    public class Qwerty{
      static int[] bubbleSort(String arr[][]) {
      int n = arr.length;
      int temp = 0;
      int []myAge = new int[n];
      for (int i = 0; i < n; i++)
      {
          myAge[i] = Integer.parseInt(arr[i][1]);
      }
      boolean sorted = false;
      while (!sorted){
          sorted = true;
          for(int i = 1; i < n; i++) {
                if(myAge[i-1] > myAge[i]) {
                   temp = myAge[i-1];
                   myAge[i-1] = myAge[i];
                   myAge[i] = temp;
                   sorted = false;
                }
          }
      }
      return myAge;
    }
    public static void main(String[] args) {
    
       Qwerty bs = new Qwerty();
       String arr [][] = {{"Ace","10"},
                         {"Ben","8"},
                         {"Cid","20"},
                         {"Dan","5"}, 
                         {"Eve","12"}};
       int myAge[] = bs.bubbleSort(arr);
       for(int i = 0; i < arr.length; i++) {
         System.out.println(arr[i][0]+ myAge[i]);
       }
    }
    

    Firstly, when you did Integer.parseInt, you were only doing it for j, and not j-1, which means that in the first iteration you would be comparing 8 with 0 (which is the default value for an int array element) when you did if(myAge[j-1] > myAge[j]). I made this more clear by just parsing the array in a separate loop beforehand.

    Secondly, in your bubble sort, the way you know that the sorting is finished is when you are able to go through the array without making any swaps. That's what I did with the sorted boolean.

    Thirdly, in your main method, when you just do bs.bubbleSort(arr), nothing will happen because the method returns void. So, I changed the method to return an int array and saved it to the myAge variable in the main method.

    I hope this helps.

    EDIT: OOPS, after reading your comment I see that you also want the names to correspond with each number, my bad. Here's the new code, and I'll explain what I changed.

    public class Qwerty{
      static String[][] bubbleSort(String arr[][]) {
      int n = arr.length;
      int tempAge = 0;
      String tempName = "";
      int []myAge = new int[n];
      String myName[] = new String [n];
      for (int i = 0; i < n; i++)
      {
          myAge[i] = Integer.parseInt(arr[i][1]);
          myName[i] = arr[i][0];
      }
      boolean sorted = false;
      while (!sorted){
          sorted = true;
          for(int i = 1; i < n; i++) {
                if(myAge[i-1] > myAge[i]) {
                   tempAge = myAge[i-1];
                   myAge[i-1] = myAge[i];
                   myAge[i] = tempAge;
                   tempName = myName[i-1];
                   myName[i-1] = myName[i];
                   myName[i] = tempName;
                   sorted = false;
                }
          }
      }
      for (int i = 0; i < arr.length; i++)
      {
          arr[i][0] = myName[i];
          arr[i][1] = Integer.toString(myAge[i]);
      }
      return arr;
    }
    public static void main(String[] args) {
    
       Qwerty bs = new Qwerty();
       String arr [][] = {{"Ace","10"},
                         {"Ben","8"},
                         {"Cid","20"},
                         {"Dan","5"}, 
                         {"Eve","12"}};
       arr = bs.bubbleSort(arr);
       for(int i = 0; i < arr.length; i++) {
         System.out.println(arr[i][0]+ arr[i][1]);
       }
    }
    
    

    I made the bubbleSort method return the sorted String[][] instead of just an int array like before. I created a separate array for the names which is the myName array. This is so that in the bubble sort, whenever a swap is made between 2 ages, the names are swapped as well, using the tempName variable. After all swaps are made to the myName and myAge arrays, I use a loop to put them back into the String arr[][], and then return it.

    Sorry about that, I should have read your question more carefully. Thanks.