-2

I am a student in a beginning java class, I got some help earlier today on my assignment, which really helped! So I thought I would give it one more try, before I throw in the towel on this last part. I have been able to get everything going, but my sort just doesn't work. I have to use this format, as my professor does not want us to use sort APIs. It processes correctly, meaning I get the same results by hand as when I run it, so I think the problem is in the logic itself. Can anyone see what I am doing wrong and offer any hints or helps. Thanks in advance. Here is my code for my sort loop:

    int i, j; // used to index into the array
    double temp;

    for (i = 1; i < count ; ++i) {
        temp = students[i].getGPA();
        j = i - 1;
        while (j >= 0 && temp < students[j].getGPA()) 
        {
            students[j + 1] = students[j];
            j = j - 1;
        }
        students[j + 1]= students[i];
    }
ug_
  • 11,267
  • 2
  • 35
  • 52

3 Answers3

1

Your are not doing the swapping correctly. Check this sample code:

        for (int i = 0; i < students.length; i++) 
        {
            for (int j = 1; j < students.length - i; j++)
             {
               if (students[j - 1].getGPA() > students[j].getGPA())
                {
                   // assuming that your class name is Student
                   Student temp = students[j - 1];
                   students[j - 1] = students[j];
                   students[j] = temp;
                }
             }
        }
manan
  • 1,385
  • 13
  • 23
Salman
  • 1,236
  • 5
  • 30
  • 59
  • Great! I used this way, at first it threw an error, but I just had to change the 2nd for loop to one less that students.length and it worked great. Thank you very much!!! – user2780236 May 05 '14 at 04:44
0

Your problem is that the first iteration through the while loop overwrites students[i]. You need to keep students[i], and not just its GPA, in a temporary value when you enter the for loop.

Warren Dew
  • 8,790
  • 3
  • 30
  • 44
  • Thank you for your answer. I tried that previously but I was having difficulty getting the data types to match up. When I made temp an object like students[I], it would let me compare them with"<". I appreciate your help. – user2780236 May 05 '14 at 04:40
0

Refined your logic little bit. Plz verify for appropriateness!

    package com.kvvssut.misc;

    public class SortArray {

        public static void main(String[] args) {

            double [] studentsGPA = {8.3,7.2,10,6.5,4.9};
            int count = studentsGPA.length;
            int i, j; // used to index into the array
            double temp;

            for (i = 1; i < count ; ++i) {
                j = i;
                while (j > 0 && (temp = studentsGPA[j]) < studentsGPA[--j]) {   // can use students[j].getGPA() similarly 
                    studentsGPA[j+1] = studentsGPA[j];
                    studentsGPA[j] = temp;
                }
            }

            for (double sgpa : studentsGPA ) {
                System.out.println(sgpa);
            }
        }

    }
53by97
  • 425
  • 3
  • 17