递归BubbleSort给出错误的输出

时间:2014-12-15 07:44:34

标签: c recursion bubble-sort

我正在尝试为结构数组实现递归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)  

2 个答案:

答案 0 :(得分:4)

字符串应该由strcmp()而不是比较运算符进行比较。

您可以将IsStructGreaterThan()更改为

int IsStructGreaterThan(struct Employee *first, struct Employee *second)
{
    return strcmp(first->name, second->name);
}

答案 1 :(得分:1)

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