简体   繁体   中英

Searching a string by binary search

#include <stdio.h>
#include <string.h>

typedef struct {
    char name[11];
    int score;
} report;

int main() {
    int n = 3;
    report student[n];
    for (int i = 0; i < 3; i++) {
        scanf("%[^\#]#%d", student[i].name, &student[i].score);
    }

    // Input name that we search.
    char search[11];
    scanf("%s", search);

    // bubble sort
    for (int a = 0; a < n - 1; a++) {
        for (int b = 0; b < n - 1 - a; b++) {
            if (student[b].score < student[b+1].score) {
                report temp;
                strcpy(temp.name, student[b].name);
                temp.score = student[b].score;

                strcpy(student[b].name, student[b+1].name);
                student[b].score = student[b+1].score;

                strcpy(student[b+1].name, temp.name);
                student[b+1].score = temp.score;
            }
        }
    }

    // binary search 
    int left = 0;
    int right = n - 1;
    int middleIndex;
    int rank;
    while (left <= right ) {
        middleIndex = (int)(left + right) / 2;
        if (strcmp(student[middleIndex].name, search) == 0) {
            rank = middleIndex+1;
            break;
        } else if (strcmp(student[middleIndex].name, search) > 0) {
            left = middleIndex + 1;
        } else if (strcmp(student[middleIndex].name,search) < 0) {
            right = middleIndex - 1;
        }
    }

    // Rank of the student's name that we search.
    printf("%d", rank);
    return 0;
}

I want to create a program that will return a student ranking (from 3 students). The fourth line is the name that we searched. I put all the user input into a struct and sort it in descending order to represent students ranking. But the problem is, when it reach the binary search, it always return unexpected value. Could you guys help me solve the problem?

Sample Input:

Jojo#40
Ray#60
Liz#80
Jojo  -> name that we searched.

""" [ {Liz, 80}, {Ray, 60}, {Jojo,40} ] """

Output: 3

At least one problem is that your names (except the first) will have a newline as the first character. That newline was left in the input stream when scanning the score.

Consequently your string compare doesn't work.

Add this

for (int a = 0; a < 3; a++) printf("|%s|\n", student[a].name);
printf("|%s|\n", search);

just after scan of search and you get the output:

|Jojo|
|
Ray|
|
Liz|
|Jojo|

As you can see there are "unexpected" newlines in front of both "Ray" and "Liz"

To solve that add a space here

scanf(" %[^\#]#%d"
       ^
       space

As noted by @EricPostpischil in a comment, sorting by score and doing binary search by name makes no sense. The sort and the search must be based on the same.

BTW: When sorting arrays use qsort

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