无法按字母顺序对结构数组进行排序

Can't sort array of structures alphabetically

我正在尝试按字母顺序对结构数组中的字符串进行排序然后打印,但我的代码不起作用。

过去几个小时我一直在试图找出原因,但无法弄清楚。我敢肯定它可能是非常明显的东西,但我只编程了几个星期而且我无法弄清楚。

它编译没有错误,输出只是原始未排序数组的打印,但没有 aardvark,像这样:男孩 敏锐 恶作剧 猫 词缀 琼脂 嗨 鹭 半开

到目前为止,这是我的代码:

#include <stdio.h>

struct entry
{
    char    word[15];
    char    definition[50];
};
struct entry dictionary[100] = 
  { {"boy",         "a boy          "                   },
    {"aardvark",    "a burrowing African mammal"        },
    {"acumen",      "mentally sharp; keen"              },
    {"addle",       "to become confused"                },
    {"cat",         "a cat"                             },
    {"affix",       "to append; attach"                 },
    {"agar",        "a jelly made from seaweed"         },
    {"ahoy",        "a nautical call of greeting"       },
    {"aigrette",    "an ornamental cluster of feathers" },
    {"ajar",        "partially opened"                  } 
  };



int main(void)
{
    int i;
    void dictionarySort(struct entry dictionary[]);

    dictionarySort(dictionary);

    for(i = 0; i < 10; ++i)
    {
        printf("%s\n", dictionary[i].word);
    }

    return 0;
}

void dictionarySort(struct entry dictionary[])
{
    int i, k, j;
    struct entry temp[100];

    for(i = 0; i <=  9; ++i)
    {
        for( k = 0; dictionary[i].word[k] != '[=10=]'; ++k)
        {
            if( (dictionary[i].word[k] > dictionary[i+1].word[k] ) )
            {
                temp[i] = dictionary[i];
                dictionary[i] = dictionary[i+1];
                dictionary[i+1] = temp[i];
            }

        }

    }
}

如果有人有任何意见,我将不胜感激。

首先,您尝试构建的算法不是排序。你在这里得到的是(在解决下面描述的问题之后)冒泡排序的一次迭代。要使其真正对数组进行排序,您需要调用 dictioarySort 10 次。有关详细信息,请参阅 https://en.wikipedia.org/wiki/Bubble_sort

现在讨论代码中的其他问题。您可以仅使用 strcmp:

来简化整个循环
for(i = 0; i <=  9; ++i)
{
    if( strcmp(dictionary[i].word, dictionary[i+1].word ) > 0 )
    {
        temp[i] = dictionary[i];
        dictionary[i] = dictionary[i+1];
        dictionary[i+1] = temp[i];
    }
}

但是如果你正在做某种练习并且想弄清楚如何按照你的方式去做,那么你的逻辑有两个问题:

  1. 考虑单词 "azc" 和 "brc"。它们按字母顺序排列,因此无需交换。在你看到它们的第一个字符,ab 对应之后,你应该停止比较它们。相反,你继续下一个字母,相应地 zr,并决定根据那个交换它们,导致错误的顺序。

  2. 你交换了两个字之后,你也应该停下来。考虑 zarb 的情况。查看第一个字母 zr 后,您将交换单词(这很好)。但是你会看到第二个字母。这次单词已经交换了,因此您将查看 ba,然后再次交换它们。所以完整的解决方案将遵循:

for(i = 0; i <=  9; ++i)
{
    for( k = 0; dictionary[i].word[k] != '[=11=]'; ++k)
    {
        if( (dictionary[i].word[k] > dictionary[i+1].word[k] ) )
        {
            temp[i] = dictionary[i];
            dictionary[i] = dictionary[i+1];
            dictionary[i+1] = temp[i];
            break; // <<-- this is new
        }
        else if( (dictionary[i].word[k] < dictionary[i+1].word[k] ) )
        {
            break; // <<-- this is new
        }
    }
}

使用 strcmp() 比较字符串

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

struct Entry {
    char    word[15];
    char    definition[50];
};

struct Entry dictionary[100] =  {
    {"boy",         "a boy          "                   },
    {"aardvark",    "a burrowing African mammal"        },
    {"acumen",      "mentally sharp; keen"              },
    {"addle",       "to become confused"                },
    {"cat",         "a cat"                             },
    {"affix",       "to append; attach"                 },
    {"agar",        "a jelly made from seaweed"         },
    {"ahoy",        "a nautical call of greeting"       },
    {"aigrette",    "an ornamental cluster of feathers" },
    {"ajar",        "partially opened"                  }
};



int main(void) {
    int i;
    void dictionarySort(struct Entry dictionary[]);
    dictionarySort(dictionary);
    for(i = 0; i < 10; ++i) {
        printf("%s\n", dictionary[i].word);
    }
    return 0;
}

void dictionarySort(struct Entry dictionary[]) {
    int i, j;
    char temp[100];
    for(i = 0; i <=  9; ++i) {
        for(j = i + 1; j <= 9; j++) {
            if(strcmp(dictionary[i].word, dictionary[j].word) > 0) {
                strcpy(temp, dictionary[i].word);
                strcpy(dictionary[i].word, dictionary[j].word);
                strcpy(dictionary[j].word, temp);
            }
        }
    }
}