结构体数组中书籍标题的冒泡排序实现问题求助
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 thetitlearray (char title[no_books][MAX_TITLE_LENGTH+1];). At this point,no_booksholds 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_booksshould be a parameter passed tomenu_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
sortedflag 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-whileloop condition is missing; it should check ifswaps > 0(or ifsortedis 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 Bookarray andno_booksas parameters so the function has access to the actual book data. - Use a
struct Booktemporary variable to swap full book records instead of just titles. - Use
strcmp()to compare titles:strcmp(a, b) > 0meansacomes afterbalphabetically, so we swap them to put them in order. - Adjust the for loop to run up to
no_books - 1to prevent accessingbooks[i+1]wheniis the last index. - Properly track swaps to exit the loop early once the array is sorted.
内容的提问来源于stack exchange,提问作者John
相关产品推荐
相关产品推荐

