简体   繁体   中英

Java - Bubble Sort Objects in Array

SOLVED Fixed code at the end of post along with progression

I apologize in advance if my question seems unclear or if I leave anything out. I'm new to programming and java, so I may not know how to phrase my questions appropriately yet. I'm going to set up what you should know in order to help me with my question, and then ask it. I'll continue editing this post if I can make things more clear.

I have an array, arrStudents, of (7) Student objects: (firstName, lastName, grade) (String, String, int)

getGrade() returns the grade

I want to create a method that bubble sorts the array and prints , so for example, this is the output prior to sort:

John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65

and here is the sorted output

Sue Taylor 55
George Bush 58
John Miller 65
Ann Miller 75
Al Clark 80
John Smith 90
Barack Obama 95

I have some code to work from, but it hasn't gotten me anywhere. Here's the example I was given with bubble sort:

import java.util.Scanner;

class array {

public static void main (String[] args) 
{

Scanner scan = new Scanner(System.in);

int [] a = new int [100];
int i, n = 0, min, max;
float  total = 0;

System.out.println ("Enter integers separated by blanks (<Enter> <Ctrl-Z> to end):");

while (scan.hasNext()) {
   a[n] = scan.nextInt();
   n = n + 1;
}

min = a[0];
max = a[0];
for (i = 0; i < n; i++) {
  if (max < a[i]) max = a[i];
  if (min > a[i]) min = a[i];
  total = total + a[i];
}

System.out.print ("You entered " + n + " numbers: ");
System.out.print ("Min = " + min + ", Max = " + max + ", Average = " + total/n);

int t, swap = 0;
do { 
    swap = 0; 
    for (i=0; i<n-1; i++) 
       if (a[i]>a[i+1]) {
           t=a[i]; 
           a[i]=a[i+1]; 
           a[i+1]=t; 
           swap++;
       }
} while (swap>0);

System.out.print ("\nSorted: ");
for (i=0; i<n; i++) System.out.print (a[i] + " ");
System.out.println ();

 }
}

I'm unable to use the array of objects with the > operator, so II tried using arrStudents[i].getGrade() , but I'm not sure if that was correct, I couldn't get it to give me the correct output.

Here's the code I've been playing with:

public static void bubbleSort(Student [] arrStudents) {
int t, swap = 0;

do {
    swap = 0;
    for (i=0; i<arrStudents.length-1; i++){
    int grade = arrStudents[i].getGrade();
    int gradePlus = arrStudents[i+1].getGrade();
       if (grade>grade+1) {
           t=grade;
           grade=gradePlus;
           gradePlus=t;
           swap++;              
       }
    }
} while (swap>0); 
}

Edit: Revised Code (Still needs fixing)

public static void bubbleSort(Student [] arrStudents) {
int swap = 0;

do {
    swap = 0;
    for (i=0; i<arrStudents.length-1; i++){
    int grade = arrStudents[i].getGrade();
    int gradePlus = arrStudents[i+1].getGrade();
       if (grade>gradePlus) {
        Student tmp = arrStudents[i];
        arrStudents[i] = arrStudents[i+1];
        arrStudents[i+1]=tmp;
        swap++;
        System.out.println(tmp);
       }          
    }
} while (swap>0);
}

Output of revised code (Descending order is fine):

Barack Obama 95
Barack Obama 95
Barack Obama 95
Barack Obama 95
Barack Obama 95
John Smith 90
John Smith 90
John Smith 90
John Smith 90
John Smith 90
Al Clark 80
Al Clark 80
Al Clark 80
Al Clark 80
Ann Miller 75
Ann Miller 75 

Fixed Code (with Ascending Output) - I was just stupid with the print from my previous attempt

public static void bubbleSort(Student [] arrStudents) {
int swap = 0;    
do {
    swap = 0;
    for (i=0; i<arrStudents.length-1; i++){
      Student tmp = arrStudents[i];  
      int grade = arrStudents[i].getGrade();
      int gradePlus = arrStudents[i+1].getGrade();
       if (grade>gradePlus) {
        arrStudents[i] = arrStudents[i+1];
        arrStudents[i+1]=tmp;
        swap++;
       }          
    }
} while (swap>0);    
System.out.print ("\nSorted: ");
for (i=0; i<arrStudents.length; i++) 
System.out.print ("\n" + arrStudents[i]);
}

Output

Sorted:
Sue Taylor 55
George Bush 58
John Miller 65
Ann Miller 75
Al Clark 80
John Smith 90
Barack Obama 95

Been stumped for awhile on this one, so any help would be much appreciated!

tl;dr Help me fix the closest above bubble sort code

Edit: Also aware that there may be better ways to sort, but for this program, I need to use bubble sort.

int grade = arrStudents[i].getGrade();
int gradePlus = arrStudents[i+1].getGrade();
   if (grade>grade+1) {

In this code you're saying that grade is greater than grade+1 which will never be true, you wanted to write the following I think

int grade = arrStudents[i].getGrade();
int gradePlus = arrStudents[i+1].getGrade();
   if (grade>gradePlus ) {

The actual sorting in this sort happens in the swap part. The elements if the array you sort have to be moved for the sort to do anything.

You have to swap arrStudents[i] and arrStudents[i+1] , since it is the arrStudents that you are sorting:

Student tmp = arrStudents[i];
arrStudents[i] = arrStudents[i+1];
arrStudents[i + 1] = tmp;

Then (also pointed out by @maczikasz), you test condition is wrong. Use:

if (grade > gradePlus) {
    // Do the swap as above, increment the swap counter
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM