C 气泡排序赢了';我不能正常工作

C 气泡排序赢了';我不能正常工作,c,sorting,C,Sorting,我尝试使用冒泡排序对一组字符串进行排序,但由于某些原因,它无法正常工作。它只是打印字符串。我已经试着解决它,但问题仍然存在。我能做什么?我想按字母顺序对字符串排序 #include<stdio.h> #include<string.h> int main(){ int i,j,count; char str[20][50],temp[20]; printf("How many strings u are

我尝试使用冒泡排序对一组字符串进行排序,但由于某些原因,它无法正常工作。它只是打印字符串。我已经试着解决它,但问题仍然存在。我能做什么?我想按字母顺序对字符串排序

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

    int main(){
       int i,j,count;
       char str[20][50],temp[20];
       printf("How many strings u are going to enter?: ");
       scanf("%d",&count);

       printf("Enter Strings one by one: ");
       for(i=0;i<=count;i++)
          gets(str[i]);


       bubbleSort(str ,count);

       printf("Order of Sorted Strings:");
       for(i=0;i<=count;i++)
          puts(str[i]);

       return 0;
    }

    void bubbleSort(char *str[], int count){
 int i, j;

 for(i = 1; i < count; i++){
    for(j = 1; j < count; j++){
        if (strcmp(str[j - 1], str[j]) > 0) {
            swap(str[j], str[j-1]);
        }
    }
}


 return;
}
void swap(char *x, char *y){
    char temp[20];
  strcpy(temp, x);
  strcpy(x, y);
  strcpy(y, temp);

 return;
}
#包括
#包括
int main(){
int i,j,计数;
字符str[20][50],temp[20];
printf(“您要输入多少字符串?:”;
scanf(“%d”、&count);
printf(“逐个输入字符串:”;

对于(i=0;i而言,问题在于
str[j-1]>str[j]
不会比较字符串值。它会比较字符串值的内存地址。由于所有内容都在连续内存中的数组中,因此
str[j-1]
地址将始终小于
str[j]
而且任何东西都不会被交换


您需要使用
strcmp

对字符串的值进行比较,那么我是否应该不使用swap函数进行比较?不,您仍然需要a进行交换。您只需要比较字符串的值而不是指针。我编辑了我的代码…我关闭了吗?现在在您的swap函数中,
strcpy
x和y已经是指针了无需遵从它们。另外,当您将字符串传递到交换函数
str[j]
str[j-1]
中时,已经指向该字符串,因此您应该将swap作为
swap(str[j],str[j-1])
调用。我按您所说的做了,但在键入字符串后,程序崩溃