You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

结构体数组中书籍标题的冒泡排序实现问题求助

Issues in Your Bubble Sort Implementation for Book Structs

Hey, let's break down the problems in your code that are stopping you from sorting books by their title alphabetically:

1. Critical Variable Scope & Initialization Mistakes

  • You declared int no_books; after using it to define the title array (char title[no_books][MAX_TITLE_LENGTH+1];). At this point, no_books holds an uninitialized garbage value, which will cause undefined behavior—either a compile error if your compiler doesn't support C99 variable-length arrays, or a broken array with a random size.
  • Worse, no_books should be a parameter passed to menu_print_database—right now, the function has no way of knowing how many books actually exist in your database.

2. You're Sorting the Wrong Data

You created a separate title array, but this disconnects the book title from its associated author and year. When you sort just the titles, you lose the link to the rest of the book's data. You should be sorting the original struct Book array directly instead.

3. Incorrect Temporary Swap Variable

Your temp variable is sized for author names, but you need a temporary struct Book to hold entire book records during swaps. Using a char array here won't let you swap full book entries.

4. Incomplete Bubble Sort Logic

  • The sorted flag isn't being initialized or updated. Bubble sort relies on this flag to exit early once the array is sorted.
  • You're missing the string comparison logic: to compare title strings alphabetically, you need to use strcmp() from <string.h>—you can't directly compare C strings with == or raw </> operators.
  • The do-while loop condition is missing; it should check if swaps > 0 (or if sorted is false).

Fixed Code Example

Here's how to adjust your function to fix these issues:

#include <string.h> // Required for strcmp()

#define MAX_TITLE_LENGTH 100
#define MAX_AUTHOR_LENGTH 50

struct Book {
    char title[MAX_TITLE_LENGTH+1];
    char author[MAX_AUTHOR_LENGTH+1];
    int year;
};

void menu_print_database(struct Book books[], int no_books) {
    int i;
    int swaps;
    struct Book temp; // Temporary struct to hold book during swap

    // Bubble sort implementation
    do {
        swaps = 0;
        for (i = 0; i < no_books - 1; i++) { // Stop at no_books-1 to avoid out-of-bounds
            // Compare current book's title with the next one
            if (strcmp(books[i].title, books[i+1].title) > 0) {
                // Swap the entire book structs
                temp = books[i];
                books[i] = books[i+1];
                books[i+1] = temp;
                swaps++;
            }
        }
    } while (swaps > 0); // Keep sorting until no swaps occur

    // Optional: Print the sorted database
    printf("Sorted Books by Title:\n");
    for (i = 0; i < no_books; i++) {
        printf("Title: %s | Author: %s | Year: %d\n", 
               books[i].title, books[i].author, books[i].year);
    }
}

Key Fixes Explained:

  • Pass the original struct Book array and no_books as parameters so the function has access to the actual book data.
  • Use a struct Book temporary variable to swap full book records instead of just titles.
  • Use strcmp() to compare titles: strcmp(a, b) > 0 means a comes after b alphabetically, so we swap them to put them in order.
  • Adjust the for loop to run up to no_books - 1 to prevent accessing books[i+1] when i is the last index.
  • Properly track swaps to exit the loop early once the array is sorted.

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:59:30