如何在C中插入字符串排序数组?
如何使用插入排序算法对字符串数组进行排序?实际上我有3个字符串数组-->如何在C中插入字符串排序数组?,c,arrays,string,sorting,insert,C,Arrays,String,Sorting,Insert,如何使用插入排序算法对字符串数组进行排序?实际上我有3个字符串数组-->a,b,c,我想根据数组a对它们进行并行排序。我已经编写了我认为正确的代码,但给出了一个错误: int main() { /* the insertsort call is */ InsertSort (count, A, B, C); /* A, B, and C are arrays of strings */ /* count is the size of the array */ } void InsertSo
a
,b
,c
,我想根据数组a对它们进行并行排序。我已经编写了我认为正确的代码,但给出了一个错误:
int main() {
/* the insertsort call is */ InsertSort (count, A, B, C);
/* A, B, and C are arrays of strings */
/* count is the size of the array */
}
void InsertSort (int n, char *a[17], char *b[17], char *c[5]){
char *temp = malloc(17);
char *temp1 = malloc(17);
char *temp2 = malloc(5);
int i, j;
for (i=1; i<n; i++) {
j=i-1;
while (j>=0 && strcmp(a[j],a[j+1])<0) {
strcpy(temp, a[j]);
strcpy(a[j], a[j+1]);
strcpy(a[j+1], temp);
strcpy(temp1, b[j]);
strcpy(b[j], b[j+1]);
strcpy(b[j+1], temp1);
strcpy(temp2, c[j]);
strcpy(c[j], c[j+1]);
strcpy(c[j+1], temp2);
j--;
}
}
}
intmain(){
/*insertsort调用是*/insertsort(count,A,B,C);
/*A、B和C是字符串数组*/
/*count是数组的大小*/
}
void InsertSort(int n,char*a[17],char*b[17],char*c[5]){
char*temp=malloc(17);
char*temp1=malloc(17);
char*temp2=malloc(5);
int i,j;
对于(i=1;i=0&&strcmp(a[j],a[j+1]),请添加对InsertSort
的调用,包括所传递参数的定义。您知道strcpy
不分配任何内存?将未初始化的指针(temp
)传递给它是UB。我建议阅读本文以更好地了解如何调试它。我认为..这篇文章不适合我的情况…无论如何,我仍然无法理解您正在尝试对三个char*
数组进行排序,但其中只有一个(a
)实际上用于键入排序,对吗?排序A
时发生的交换应针对B
和C
中的相同索引进行,对吗?和fwiw,除了分别为A、B和C进行的交换之外,不需要动态分配。