gpt4 book ai didi

c - 递归 BubbleSort 给出错误的输出

转载 作者:太空宇宙 更新时间:2023-11-04 05:56:00 26 4
gpt4 key购买 nike

我正在尝试为结构数组实现递归冒泡排序。但是,当我按员工姓名对数组进行排序时,它给出了错误的输出。我不知道我错过了什么。感谢您的帮助。

#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)

最佳答案

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

您可以将 IsStructGreaterThan() 更改为

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

关于c - 递归 BubbleSort 给出错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27479426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com