Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 I I 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.

share|improve this question
    
So using the min/max to compare? I actually had it set up up in another one of my methods, looks something like if (student.getGrade() > max.getGrade()) max = student; –  user3594736 May 9 at 13:13
    
Where are you handling the Students names? Or can you ignore them and just sort the grades? –  Peter Lawrey May 9 at 13:15
1  
@MarcMosby: the proper way would be to implement Comparable<Student>, rather than reinventing naming conventions. –  njzk2 May 9 at 13:16
    
@PeterLawrey maxzikasz covered it in his answer. My problem was that I couldn't get arrStudents[i] to use the operators, so I was trying to get it to work with getGrade. –  user3594736 May 9 at 13:30

2 Answers 2

up vote 1 down vote accepted

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
}
share|improve this answer
    
So after swapping, I should just be able to stick System.out.println(arrStudents[i]); in there and pass it as a parameter to the main method to print, right? It's not working. Compiles and everything but I'm not getting the right output. –  user3594736 May 9 at 13:28
    
Okay, I almost got it to work now, it prints in descending order, but that's fine. The output is still really funky. Prints the student object with the biggest grade 5 times, repeats for 2nd and 3rd, and then prints the 4th student object on the list twice. I'll edit the original post. –  user3594736 May 9 at 13:39
    
Was stupid with where I stuck the print, this works! –  user3594736 May 9 at 15:10
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 ) {
share|improve this answer
    
Whoops, thanks for the catch. Been playing with the code a lot and missed that. –  user3594736 May 9 at 13:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.