我正在尝试为结构数组实现递归bubblesort。但是,当我按员工姓名对数组排序时,它给出了错误的输出。我想不出我错过了什么。如有任何帮助,我们将不胜感激。

#include<stdio.h>
#include<stdlib.h>

// GLOBAL VARIABLES
char *stringDataType = "string";
char *integerDataType = "integer";

// Employee structure
struct Employee
{
    char *name;
    int age;
};

// Method to swap two structures by reference.
void Swap(struct Employee *first, struct Employee *second)
{
    struct Employee temp = *first;
    *first = *second;
    *second = temp;
}

// Method to check if the first string is greater than second string or not
// 1 if the first string is greater
// -1 if the seond string is greater
// 0  if both the strings are equal
int IsGreaterThan(char **first , char **second)
{
    int index = 0;
    while(*((*first)+index) == *((*second)+index))
    {
        index++;
    }

    if(*((*first)+index) > *((*second)+index))
    {
        return 1;
    }
    else if(*((*first)+index) < *((*second)+index))
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

// Method to check if the first structure is greater than second structure or not
// 1 if the first structure is greater
// -1 if the seond structure is greater
// 0  if both the structure are equal
int IsStructGreaterThan(struct Employee *first, struct Employee *second)
{
    int index = 0;

    return IsGreaterThan(&(*first).name, &(*second).name);
}

// Bubble Sort Method
void BubbleSort(struct Employee array[], int size, char *dataType, int swapped)
{
    int i;

    if(swapped == 0 || size == 0)
    {
        return;
    }

    for(i = 0; i < size - 1; i++)
    {
        swapped = 0;

        if(dataType==stringDataType && (IsStructGreaterThan(&array[i], &array[i+1]) == 1) || dataType==integerDataType && array[i].age > array[i+1].age)
        {
            Swap(&array[i], &array[i + 1]);
            swapped = 1;
        }
    }

    BubbleSort(array, size-1, dataType, swapped);
}

// Entry point of the program
int main()
{
    struct Employee array[] = {{"John", 45}, {"Mary", 23}, {"Celina", 79}, {"Mike", 41}};
    int arraySize = 4;
    int index;

    printf("Before Sorting : \n");
    for(index = 0; index < arraySize; index++)
    {
        printf("(%s, %d) ", array[index].name, array[index].age);
    }

        printf("\n");

    int swapped = 1;

    BubbleSort(array, arraySize, stringDataType, swapped);
    printf("After Sorting by name : \n");
    for(index = 0; index < arraySize; index++)
    {
        printf("(%s, %d) ", array[index].name, array[index].age);
    }

        printf("\n");

    BubbleSort(array, arraySize, integerDataType, swapped);
    printf("After Sorting by age : \n");
    for(index = 0; index < arraySize; index++)
    {
        printf("(%s, %d) ", array[index].name, array[index].age);
    }

    printf("\n");
    return 0;
}

产量
Before Sorting :
(John, 45) (Mary, 23) (Celina, 79) (Mike, 41)
After Sorting by name :
(John, 45) (Celina, 79) (Mary, 23) (Mike, 41)
After Sorting by age :
(Mary, 23) (Mike, 41) (John, 45) (Celina, 79)

最佳答案

swapped = 0放在for循环之前,而不是放在循环内部,并根据其他答案修复字符串比较。

09-08 05:39