简体   繁体   中英

Sorting user defined array by string using selection sort in java

I'm working on an assignment and I need to perform a binary search. But somehow I think I have problems in my selection sort. Here i have an user defined class called Record. It has the following properties:

class Record{
   String studentId;
   int assignment;
   int exam;
   int total;
   String grade;
}

I have getters for those properties. Now there is another class called GradeBook in which there is a an array of type Record . I manually loaded the record array via a method called loadFromTables as follows:

private void loadFromTables(){

    String[] students = {
        "S10","S20","S30","S40","S50", "S60", 
        "S08","S18","S28","S38","S48", "S58",
        "S06","S16","S26","S36","S46", "S56",
    };

    int[] assignment = {
        0, 10, 20, 30, 30, 40, 
        0, 10, 20, 30, 30, 40, 
        0, 10, 20, 30, 30, 40,
    };

    int[] exam = {
        0, 39, 44, 44, 54, 59, 
        1, 40, 45, 45, 55, 60, 
        2, 41, 46, 46, 56, 58, 
    };

    nrecords = students.length;
    gradeBook = new Record[nrecords];

    for (int i = 0; i < nrecords; i++ ) {

        int t = assignment[i] + exam[i];
        String g = calculateGrade(t);
        Record r = new Record( students[i], assignment[i], exam[i], t, g );
        gradeBook[i] = r;

    }


}

Now I want to do binary search to find a Record by the property studentId . But first I'd have to sort the Record array . I am told to use Selection sort . So, I do this and I think this is where the problem lies but I can't seem to figure out where..:

private void sortById(){

    //Selection Sort

    for(int i=0; i<nrecords-1; i++){

        int index = i;

        for(int j=i+1; j<nrecords; j++){

            if((gradeBook[index].studentId).compareTo(gradeBook[j].studentId) > 0){

                index = j;

            }

            Record temp =  gradeBook[i];
            gradeBook[i] = gradeBook[index];
            gradeBook[index] = temp;

        }

    }

}

And here is the code of binary search that I used although i think the binary search has been implemented correctly. Because i tried to do it with bubble sort and that did precisely what i wanted.

public Record find(String id){

    //Binary Search

    int low = 0;
    int high = nrecords - 1;
    Record record = null;

    while(low <= high){

        int mid = (high + low)/2;

        if(id.compareTo(gradeBook[mid].studentId) == 0){

            record = new Record(id, gradeBook[mid].assignment, gradeBook[mid].exam, gradeBook[mid].total, gradeBook[mid].grade);
            return record;

        }
        else if(id.compareTo(gradeBook[mid].studentId) > 0){

            low = mid + 1;

        }
        else if(id.compareTo(gradeBook[mid].studentId) < 0){

            high = mid - 1;

        }

    }

    return record;

}

Thanks in advance. I know the problem is in selection sort and it's eating my head. Appreciate your suggestions! :)

In selection sort, We first iterate through the sub-array and find the minimum element in the sub-array, then at last in each iteration, Swap the current and minimum element.

Problem in your code is here.

for(int j=i+1; j<nrecords; j++){

    if((gradeBook[index].studentId).compareTo(gradeBook[j].studentId) > 0){
            index = j;
    }

    Record temp =  gradeBook[i];
    gradeBook[i] = gradeBook[index];
    gradeBook[index] = temp;

}

You have found minimum element correctly but you are doing swaps in the iterations when you find a lexicographic smaller string than the current one. So in this loop, you just need to find the minimum element and swap operation should be done after execution of this loop.

Corrected Code :

private void sortById(){

    //Selection Sort

    for(int i=0; i<nrecords-1; i++){

        int index = i;

        for(int j=i+1; j<nrecords; j++){

            if((gradeBook[index].studentId).compareTo(gradeBook[j].studentId) > 0){

                index = j;

            }

            Record temp =  gradeBook[i];
            gradeBook[i] = gradeBook[index];
            gradeBook[index] = temp;

        }

    }

}

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