使用java中的选择排序按字符串对用户定义的数组进行排序

时间:2017-05-14 13:40:29

标签: java arrays sorting selection-sort

我正在完成任务,我需要执行二分查找。但不知何故,我认为我的选择排序存在问题。这里我有一个名为Record的用户定义类。它具有以下属性:

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

我有这些属性的吸气剂。现在还有另一个名为 GradeBook 的类,其中有一个类型为记录的数组。我通过一个名为loadFromTables的方法手动加载记录数组,如下所示:

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;

    }


}

现在我想进行二分查找,找到属性 studentId 的记录。但首先我必须对记录数组进行排序。我被告知使用选择排序。所以,我这样做,我认为这就是问题所在,但我似乎无法弄明白......:

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;

        }

    }

}

这里是我使用的二进制搜索代码,虽然我认为二进制搜索已经正确实现。因为我试图用冒泡排序来做这件事,而这正是我想要的。

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;

}

提前致谢。我知道问题在于选择排序而且它在吃我的脑袋。感谢您的建议! :)

1 个答案:

答案 0 :(得分:1)

在选择排序中,我们首先遍历子数组并找到子数组中的最小元素,然后在每次迭代中找到Swap当前和最小元素。

您的代码存在问题。

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;

}

您已经正确找到了最小元素,但是当您找到比当前字符串更小的字典时,您在迭代中进行交换。所以在这个循环中,你只需要找到最小元素,并且在执行这个循环后应该进行swap操作。

更正后的代码:

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;

        }

    }

}