在 C 中按字母顺序对字符串和结构进行排序

Sorting Strings and Structures alphabetically in C

我正在尝试编写一个函数,它从结构中获取一组字符串并按字母顺序组织结构。这是我目前所拥有的:

// Preconditions: array of structure "Shift"
// integer value indicating number of shifts
// Postconditions: none - this function does not return anything.
// Actions: Sort the shifts by the TA's first name.

void sort_data(struct Shift shift_data[], int *num_shifts)
{
int i,j;

    for(i=0; i<num_shifts; i++)
        for (j=0; j<num_shifts; j++)
        {
        if (strcmp(shift_data[i+1].name, shift_data[i].name) < 0)
              {
                  temp[i]=shift_data[i];
                  shift_data[i]=shift_data[i+1];
                  shift_data[i]=temp[i];
              }

        }


}

我不确定是否需要嵌套循环。我确信 num_shifts 和移位数据正在被正确读取和指向。我还在我的代码顶部声明了我的结构和与结构相关的变量:

struct Shift
{
char name[100];
char day_of_week[100];
int start_hour;
int end_hour;
};

struct Shift shift_data[100];
struct Shift temp[100];

抱歉没有说明这一点,但我需要使用这三个功能:

int read_data(struct Shift shift_data[], int *num_shifts);
void sort_data(struct Shift shift_data[], int *num_shifts);
void print_data(struct Shift shift[], int *num_shifts);

我可以添加其他人,但不需要。

我需要弄清楚如何根据存储在 shift_data[i].name

中的函数中的字符串类型对结构进行排序

提前感谢您的帮助

这是完成的排序函数的样子:

// Preconditions: array of structure "Shift"
// integer value indicating number of shifts
// Postconditions: none - this function does not return anything.
// Actions: Sort the shifts by the TA's first name.

void sort_data(struct Shift shift_data[], int *num_shifts)
{
    int i, changed;
    do
    {
        changed = 0;
        for (i=0; i < (*num_shifts) - 1; i++)
        {
            if (strcmp(shift_data[i].name, shift_data[i+1].name) > 0)
            {
                memcpy(&temp, shift_data + i, sizeof (struct Shift));
                memcpy(shift_data + i, shift_data + i + 1, sizeof (struct Shift));
                memcpy(shift_data + i + 1, &temp, sizeof (struct Shift));
                changed = 1;
           }
        }
    } while (changed != 0);
}

谢谢大家的帮助

最简单的方法应该是调用 qsort().

/* for strcmp() */
#include <string.h>
/* for qsort() */
#include <stdlib.h>

int cmpShift(const void* lhs, const void* rhs)
{
    return strcmp(((struct Shift*)lhs)->name, ((struct Shift*)rhs)->name);
}

void sort_data(struct Shift shift_data[], int *num_shifts)
{
    qsort(shift_data, *num_shifts, sizeof(struct Shift), cmpShift);
}

这是一个非常基本的冒泡排序,与更高级的排序算法相比效率非常低,并且仍然复制整个结构而不是指针,但它比你开始时更接近:

struct Shift shift_data[100];
struct Shift temp;

void sort_data(struct Shift shift_data[], int *num_shifts)
{
    int i, changed;
    do
    {
        changed = 0;
        for (i=0; i < (*num_shifts) - 1; i++)
        {
            if (strcmp(shift_data[i].name, shift_data[i+1].name) > 0)
            {
                memcpy(&temp, shift_data + i, sizeof (struct Shift));
                memcpy(shift_data + i, shift_data + i + 1, sizeof (struct Shift));
                memcpy(shift_data + i + 1, &temp, sizeof (struct Shift));
                changed = 1;
           }
        }
    } while (changed != 0);
}

基本思想是它扫描整个数组,将每一项与下一项进行比较。每次它发现一个顺序不正确时,它就会交换它们。在随后的传递中,应该在前面的项目像 'bubbles' 一样浮动到前面,对于应该在后面的项目也是如此。该函数保留一个变量 changed 以确定在最后一次遍历数组时是否进行了任何更改 - 当它遍历整个数组而不更改任何内容时,循环可以退出,因为数组已排序。

请注意,当您传入指针int *num_shifts 时,您需要使用 *num_shifts 取消引用它以访问它指向的 int

read_data()print_data() 函数其实不用提,它们与排序无关,这正是你问的。

您函数的 // Postconditions: 之一肯定是数组已排序?