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

基于冒泡排序实现C语言中结构体数组的多条件排序

Solution to Multi-Condition Bubble Sort for Struct Arrays in C

Hey there! Let's work through your problem step by step. You want to extend your existing sort to handle multiple conditions and encapsulate the sorting logic into a reusable function—here's how to do it properly:

Key Fixes & Improvements

1. Multi-Condition Sorting Logic

Your current code only compares follower counts. To implement the full sorting rules, we need a layered comparison when deciding whether to swap two users:

  • First priority: Follower count descending (higher followers come first)
  • Second priority: Follow count descending (if followers are equal, more follows come first)
  • Third priority: User index ascending (if both counts are equal, lower index comes first)

For two users a and b, we swap them if:

  • a.nFollower > b.nFollower, OR
  • a.nFollower == b.nFollower AND a.nFollow > b.nFollow, OR
  • a.nFollower == b.nFollower AND a.nFollow == b.nFollow AND a.node < b.node

2. Encapsulating the Sort Function

In C, you can create a standalone sort function that takes your User array (as a pointer, since arrays decay to pointers when passed to functions) and the array length. This keeps your main function clean and makes the sort logic reusable.

3. Simplifying Struct Swaps

Instead of swapping individual members with multiple temp variables, use a temporary User struct to hold one element while swapping. This is cleaner and less error-prone.

Full Corrected Code

#include <stdio.h>
#include "WGraph.h"

typedef struct User {
    int node;
    int nFollower;
    int nFollow;
} User;

// Encapsulated sorting function
void sortUsers(User users[], int nUsers) {
    for (int i = 0; i < nUsers; i++) {
        for (int j = i + 1; j < nUsers; j++) { // Optimized to avoid redundant checks
            // Check if users[j] should come before users[i]
            if (users[j].nFollower > users[i].nFollower) {
                // Swap: higher follower count comes first
                User temp = users[i];
                users[i] = users[j];
                users[j] = temp;
            } else if (users[j].nFollower == users[i].nFollower) {
                // Followers equal: check follow count descending
                if (users[j].nFollow > users[i].nFollow) {
                    User temp = users[i];
                    users[i] = users[j];
                    users[j] = temp;
                } else if (users[j].nFollow == users[i].nFollow) {
                    // Both counts equal: check index ascending
                    if (users[j].node < users[i].node) {
                        User temp = users[i];
                        users[i] = users[j];
                        users[j] = temp;
                    }
                }
            }
        }
    }
}

int main (void) {
    Edge e = {0, 0, 1};
    int nUsers;
    printf("Enter the number of users: ");
    scanf("%d", &nUsers);
    Graph g = newGraph(nUsers);
    printf("Enter a user (follower): ");
    while (scanf("%d", &e.v) == 1) {
        printf("Enter a user (followed by %d): ", e.v); // Fixed prompt to show current follower
        scanf("%d", &e.w);
        insertEdge(g, e);
        printf("Enter a user (follower): ");
    }
    printf("Done.\n");

    int nV = numOfVertices(g);
    User u[nV];
    
    // Calculate follower and follow counts for each user
    for (int i = 0; i < nV; i++) {
        int countFollow = 0;
        int countFollower = 0;
        u[i].node = i;
        for (int j = 0; j < nV; j++) {
            if (adjacent(g, j, i)) { // j follows i → increment i's follower count
                countFollower++;
            }
            if (adjacent(g, i, j)) { // i follows j → increment i's follow count
                countFollow++;
            }
        }
        u[i].nFollower = countFollower;
        u[i].nFollow = countFollow;
    }

    // Sort the users using our encapsulated function
    sortUsers(u, nV);

    // Print the sorted result
    printf("Ranking by follower base:\n");
    for (int i = 0; i < nV; i++) {
        printf("%d has %d follower(s) and follows %d user(s).\n", u[i].node, u[i].nFollower, u[i].nFollow);
    }

    freeGraph(g); // Don't forget to free allocated graph memory!
    return 0;
}

Explanation of Key Changes

  • Sort Function: The sortUsers function takes the User array and its length, making it reusable for any User array in your code.
  • Layered Comparison: We follow your exact sorting rules, checking each condition only when higher-priority ones are equal.
  • Optimized Loop: Changed the inner loop to start at i + 1 to avoid redundant pair comparisons, turning it into a more efficient selection sort variant.
  • Cleaner Swaps: Using a temporary User variable eliminates the risk of mixing up individual struct members (a bug in your original code).
  • Fixed Input Prompt: Updated the prompt to show the current follower ID instead of hardcoding 0, making input easier to follow.

Test Result

When run with your sample input, this code will produce the exact expected output you listed.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 16:19:08