Search code examples
javasortingbubble-sort

Why is my bubble sort not working? Java


First time posting! I have an assignment due that requires me to write a java program that reads in data from a text file and sorts it according to score and their initials. The text file is as follows:

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

Here is my code:

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

}

But for some reason it always comes out as:

JW 97                                                                                                                                                            
JG 82                                                                                                                                                            
MS 80                                                                                                                                                            
JB 75                                                                                                                                                            
JD 65  

The output should be:

JW 97
JG 82
MS 80
JD 75
JB 65

Any reasons why this keeps happening?


Solution

  • This is solution as per your code. but code must be better then this. you should use collection classes. In this code you need to swap names accordingly.

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